even better column layout

This commit is contained in:
Gregory.Shrago
2012-05-24 18:41:58 +04:00
parent 80d7a2069f
commit 5882286166
2 changed files with 22 additions and 12 deletions
@@ -320,6 +320,17 @@ public class JBTable extends JTable implements ComponentWithEmptyText, Component
}
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
if (orientation == SwingConstants.VERTICAL) {
return super.getScrollableUnitIncrement(visibleRect, orientation, direction);
}
else { // if orientation == SwingConstants.HORIZONTAL
// use smooth editor-like scrolling
return SwingUtilities.computeStringWidth(getFontMetrics(getFont()), " ");
}
}
@Override
public void doLayout() {
super.doLayout();
@@ -93,8 +93,7 @@ public class TableView<Item> extends BaseTableView implements ItemsProvider, Sel
int[] sizeMode = new int[columns.length];
int[] headers = new int[columns.length];
int[] widths = new int[columns.length];
int fixedWidth = 0;
int varWidth = 0;
int allColumnWidth = 0;
int varCount = 0;
// calculate
@@ -132,18 +131,18 @@ public class TableView<Item> extends BaseTableView implements ItemsProvider, Sel
widths[i] = getFontMetrics(getFont()).stringWidth(preferredValue) + columnInfo.getAdditionalWidth();
varCount ++;
}
if (sizeMode[i] == 0) {
fixedWidth += widths[i];
}
else {
varWidth += widths[i];
}
allColumnWidth += widths[i];
}
// apply: distribute available space between resizable columns but no more than *4 times.
// apply: distribute available space between resizable columns
// and make sure that header will fit as well
int viewWidth = getParent() != null? getParent().getWidth() : getWidth();
int addendum = varCount > 0 && viewWidth > fixedWidth + varWidth? (viewWidth - fixedWidth - varWidth) / varCount : 0;
double gold = 0.5 * (3 - Math.sqrt(5));
int addendum =
varCount == 0 ? 0 : (int)((allColumnWidth < gold * viewWidth ? gold * viewWidth :
allColumnWidth < (1 - gold) * viewWidth ? (1 - gold) * viewWidth :
viewWidth) - allColumnWidth) / varCount;
for (int i=0 ; i<columns.length; i ++) {
TableColumn column = getColumnModel().getColumn(i);
int width = widths[i];
@@ -153,12 +152,12 @@ public class TableView<Item> extends BaseTableView implements ItemsProvider, Sel
column.setMinWidth(width);
}
else if (sizeMode[i] == 2) {
width = Math.max(width + Math.min(addendum, 4 * width), headers[i]);
width = Math.max(width + addendum, headers[i]);
column.setPreferredWidth(width);
column.setMaxWidth(width);
}
else if (sizeMode[i] == 3) {
width = Math.max(width + Math.min(addendum, 4 * width), headers[i]);
width = Math.max(width + addendum, headers[i]);
column.setPreferredWidth(width);
}
}