Auto Resize JTable Columns
Posted by tech on
November 15, 2009
This class auto resizes columns of a JTable, maximizing any non-used width columns and transferring those extra widths to other columns. This way, a column with a JCheckBox will have a width almost equal to that of a JCheckBox’s width. The class TableColumnResizer contains one static method and you can pass the JTable object as the parameter. The code does the rest.
To use the method, do this:
TableColumnResizer.adjustColumnPreferredWidths(myJTableObject);
Here is the code of the TableColumnResizer class.
public class TableColumnResizer { public static void adjustColumnPreferredWidths(JTable table) { if (table == null || table.getColumnCount() == 0) return; // strategy - get max width for cells in column and // make that the preferred width TableColumnModel columnModel = table.getColumnModel(); for (int col=0; col

/rating_on.png)








November 16th, 2009 at 3:25 am
i like ur script, I tray now