Auto Resize JTable Columns

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
Bookmark and Share

Found this post useful? Buy me a cup of coffee or help support the sponsors on the right.

Related Posts with Thumbnails

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
Loading ... Loading ...

One Response to “Auto Resize JTable Columns”

  1. 1
    rizky2009 Says:

    i like ur script, I tray now

Leave a Reply