Table Column Adjuster
Posted by Rob Camick on November 10, 2008
JTable sets a default width for each TableColumn in the table. When designing the table you can alter this width by setting the preferred size of the table column. However this size is only going to be a best guess as to the real width of the column. The real width of the column will never be known until data has been loaded in the table. It would be nice to be able to alter the width of the columns so that all text in the cell is displayed.
Some tables contain static data and some contain dynamic data. So we will need the ability to change the column widths after data is initially loaded and the ability to alter columns widths as the user changes the data in the table. Well, the TableColumnAdjuster can handle both situations. There are four main methods that can be used to change the column widths:
- Adjust Column – adjust the width of the specified column
- Adjust Columns – adjust the width of all columns
- Restore Column – restore the width of the specified column
- Restore Columns – restore the width of all columns
To help the TableColumnAdjuster calculate the appropriate column width you can set the following properties:
- Column Header Included – use the width of the column header
- Column Data Included – loop through all the rows of the TableModel and find the cell renderer with the largest width
- Only Adjust Larger – only reset the column width if the calculated value is greater than the current width of the column.
The maximum width calculated using the above properties will be used for the column width. To provide default column widths you might use code like the following which will generate the table shown in the image:
TableColumnAdjuster tca = new TableColumnAdjuster(table);
Above is an example of installing the column adjuster and using static adjustments. As mentioned earlier the TableColumnAdjuster also supports dynamic column adjusts using the following property:
- Dynamic Adjustment - changes to the TableModel will cause the adjustments to occur automatically. Updating a table cell will cause the column to be adjusted. Adding/removing rows will cause all columns to be adjusted.
The user may find the dynamic adjustment a bit annoying if the width changes every time they type something, so we can also give the user to option to request column adjustments. This was done by adding Actions to the table which the user can invoke with the appropriate key stroke:
|Adjust Column||Control +|
|Adjust Columns||Control Shift +|
|Restore Column||Control -|
|Restore Columns||Control Shift -|
|Toggle Dynamic||Control *|
|Toggle Larger||Control /|
Note, that this class was designed to be used with tables that use an auto resize mode of AUTO_RESIZE_OFF. With all other modes you are constrained, as the width of the columns must fit inside the table. So if you increase the width of one column, the widths of one or more of the other columns must decrease. Because of this, the resize mode of RESIZE_ALL_COLUMNS will work the best.
Hopefully the usage of the static methods along with the user controlled Actions will provide the flexibility needed to adjust the width of any column in any situation.