Pikopong

It's all about knowledge

Auto Resize JTable Column Width

View 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

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • PDF
  • Print
  • Reddit
  • Slashdot
  • Technorati
  • Twitter

Related Posts

Written by amree

August 13th, 2008 at 2:49 pm

Posted in programming

Tagged with , ,

View Comments 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

    enzoNo Gravatar

    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 :)

    amreeNo Gravatar

    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!!

    enzoNo Gravatar

    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

    amreeNo Gravatar

    25 Jul 09 at 10:39 PM

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

    WillNo Gravatar

    8 Sep 09 at 11:22 PM

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

    chutanNo Gravatar

    16 Sep 09 at 03:10 AM

  7. Good stuff. Thanks.

    mazaisNo Gravatar

    22 Sep 09 at 05:36 AM

  8. Thanks! Just exactly what I needed.

    PhilNo Gravatar

    29 Oct 09 at 08:32 AM

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

    JCarlosNo Gravatar

    12 Nov 09 at 06:45 AM

  10. Works for me. Thanks!

    RobNo Gravatar

    11 Jan 10 at 11:01 AM

  11. Hi,

    it doesn’t work for me… I copied your method and call it like this:
    1) JTable jtable = new JTable(data, columnNames); //my data and my columnNames
    2) DefaultTableModel model = new DefaultTableModel();
    3) jtable = autoResizeColWidth(jtable, model);
    When I leave out your code, my jtable is displayed with correct content but without enough cell width for the data. When I use your method as stated above, there’s no more table content (neither header nor data).

    I would be happy if someone could help me
    Luke

    LukeNo Gravatar

    2 Sep 10 at 12:19 AM

  12. Does the ‘data’ refers to your DefaultTableModel? If it is, then you don’t have to recreate another model, just put your model in the autoResizeColWidth(). The ‘model’ should be your own data which is a DefaultTableModel type, not mine.

    amreeNo Gravatar

    2 Sep 10 at 01:04 AM

Leave a Reply

blog comments powered by Disqus