pikopong

it's all about knowledge

Auto Resize JTable Column Width

with 10 comments

This code should resize your JTable column width based on the contents of the header and data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    public JTable autoResizeColWidth(JTable table, DefaultTableModel model) {
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setModel(model);
 
        int margin = 5;
 
        for (int i = 0; i < table.getColumnCount(); i++) {
            int                     vColIndex = i;
            DefaultTableColumnModel colModel  = (DefaultTableColumnModel) table.getColumnModel();
            TableColumn             col       = colModel.getColumn(vColIndex);
            int                     width     = 0;
 
            // Get width of column header
            TableCellRenderer renderer = col.getHeaderRenderer();
 
            if (renderer == null) {
                renderer = table.getTableHeader().getDefaultRenderer();
            }
 
            Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
 
            width = comp.getPreferredSize().width;
 
            // Get maximum width of column data
            for (int r = 0; r < table.getRowCount(); r++) {
                renderer = table.getCellRenderer(r, vColIndex);
                comp     = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false,
                        r, vColIndex);
                width = Math.max(width, comp.getPreferredSize().width);
            }
 
            // Add margin
            width += 2 * margin;
 
            // Set the width
            col.setPreferredWidth(width);
        }
 
        ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(
            SwingConstants.LEFT);
 
        // table.setAutoCreateRowSorter(true);
        table.getTableHeader().setReorderingAllowed(false);
 
        return table;
    }

Example:

1
2
3
// Must pass the model
DefaultTableModel model = new DefaultTableModel();
jTable = autoResizeColWidth(jTable, model);

Tested on JRE v5 and JRE v6

Written by amree

August 13th, 2008 at 2:49 pm

Posted in programming

10 Responses to 'Auto Resize JTable Column Width'

Subscribe to comments with RSS or TrackBack to 'Auto Resize JTable Column Width'.

  1. Hi,
    I would like try your code. But I an not able because I don’t find the DefaultTableColour class ad code go on error naturally. I find it in all java package , bun don’t find it. Could you tell me where is it? Thanks
    Enzo

    enzo

    23 Jul 09 at 02:55 PM

  2. @enzo

    Oops.. sorry about that, it was my custom class for colouring the table cells and has nothing to do with auto resizing the table, I’ve removed it. Please try it again, good luck :)

    mree

    23 Jul 09 at 06:47 PM

  3. Fantastics. Now it’s working very well!!!!
    Thanks Enzo

    —-
    A little advice: it’s very difficult read your captcha, because foreground and background colour are similar!!

    enzo

    25 Jul 09 at 01:57 AM

  4. @enzo

    I had lots of spam before, that’s why I use that captcha, maybe I’ll change to something else, but thanks for the advice :D

    mree

    25 Jul 09 at 10:39 PM

  5. Seriously, you rock dude!!!
    Been looking for something like this for a while :D

    Will

    8 Sep 09 at 11:22 PM

  6. I think you need to convert view column index to model column index in line 10.

    chutan

    16 Sep 09 at 03:10 AM

  7. Good stuff. Thanks.

    mazais

    22 Sep 09 at 05:36 AM

  8. Thanks! Just exactly what I needed.

    Phil

    29 Oct 09 at 08:32 AM

  9. Great my friend, just what I was looking for. Thanks.

    JCarlos

    12 Nov 09 at 06:45 AM

  10. Works for me. Thanks!

    Rob

    11 Jan 10 at 11:01 AM

Leave a Reply