Often, we want to automatically widen a column in a table to make if fill
out any extra space when the table control is resized.
This should be pretty easy to do, right? We can just take the available
space, subtract the other column widths, see what's left over and then
decide whether we should plump up a certain column.
But, what about the vertical scroll bar? It takes width too. So, we
have to see if it exists and if it is visible, and then account for its
width too.
The right API's would seem to be:
ScrollBar sb = table.getVerticalBar();
and
sb.getVisible();
The code sample below illustrates this but it doesn't work because
getVisible always returns true, NO MATTER WHAT.
Anyone know a solution to this problem? Our next attempt would be to do
something really hideous like trying to use one of the API's to tell us
what column a certain point is in to indirectly determine whether the
scrollbar is there (yeccchh).
TIA, Bill.
=====================================
private void maximizeTableColWidth(Table t, int column, int minWidth) {
// Determine overall width available for use by the table
int availableWidth = t.getSize().x - t.getBorderWidth() * 2;
// Subtract the widths of all columns except the one to be maximized
for (int i = 0; i < t.getColumns().length; i++) {
if (i != column ) {
availableWidth -= t.getColumn(i).getWidth();
}
}
// If vertical scrollbar is present, subtract its width too
if (t.getVerticalBar() != null) {
if (t.getVerticalBar().getVisible()) {
availableWidth -= t.getVerticalBar().getSize().x;
}
}
// Set the column to avail width or its minimum - whichever is larger
t.getColumn(column).setWidth( availableWidth > minWidth ?
availableWidth : minWidth);
}