Automatically adjust Jtable Column to fit content
Asked Answered
E

4

13

I am trying to match the JTable column width depending on the data inside. My Code:

    for(int column = 0; column < gui.testsuiteInfoTable.getColumnCount(); column ++){
        int  width =0;
    for (int row = 0; row < gui.testsuiteInfoTable.getRowCount(); row++) {
         TableCellRenderer renderer = gui.testsuiteInfoTable.getCellRenderer(row, column);
         Component comp = gui.testsuiteInfoTable.prepareRenderer(renderer, row, column);
         width = Math.max (comp.getPreferredSize().width, width);
         System.out.println(width);
     }
    TableColumn col = new TableColumn();
    col = gui.testsuiteInfoTable.getColumnModel().getColumn(column);
    System.out.println(width);
    col.setWidth(width);
    gui.testsuiteInfoTable.revalidate();

    }
}

The sizes are correct I guess but the table columns still all have the same width! The table is embedded in a ScrollPane in a GridBagLayout is that the problem? Thanks for any suggestions.

Entitle answered 25/7, 2013 at 12:33 Comment(1)
Look at linkGranitite
A
12

If you can use an extra library, try Swingx (https://java.net/projects/swingx) There you have a JXTable, with a method "packAll()", that does exactly what you are asking for

Audet answered 25/7, 2013 at 14:4 Comment(0)
F
25

This is all you need:

JTable table = new JTable(){
    @Override
       public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
           Component component = super.prepareRenderer(renderer, row, column);
           int rendererWidth = component.getPreferredSize().width;
           TableColumn tableColumn = getColumnModel().getColumn(column);
           tableColumn.setPreferredWidth(Math.max(rendererWidth + getIntercellSpacing().width, tableColumn.getPreferredWidth()));
           return component;
        }
    };
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

The table will adjust each column width automatically to fit the content. There's no need to control when to trigger the resizing.

Floris answered 29/8, 2014 at 14:51 Comment(2)
That'll works, but you'll get a warning asking for serialVersionUID if you don't add it. +1.Oestrone
It looks like once the table column gets resized to a larger value, it doesn't shrink back down to fit the current state of columns.Sherburn
A
12

If you can use an extra library, try Swingx (https://java.net/projects/swingx) There you have a JXTable, with a method "packAll()", that does exactly what you are asking for

Audet answered 25/7, 2013 at 14:4 Comment(0)
K
6
col.setWidth(width);

Read the JTable API and follow the link on How to Use Tables. In that tutorial they use the setPreferredWidth(...) to suggest a width for a column.

You may also want to check out the Table Column Adjuster which does this for you. This solution can also take into account the width of the column header.

Kola answered 25/7, 2013 at 14:44 Comment(1)
Thank you. The problem seems to be if you don´t add the JTable to a JScrollPane, then setWidth etc. does not work.Entitle
L
0

I would like to share my solution:

public class AutoResizedTable extends JTable {
    {
        setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    }

    @Override
    public void tableChanged(TableModelEvent e) {
        super.tableChanged(e);
        resizeAndRepaint();
    }

    @Override
    public void doLayout() {
        for(int column = 0; column < getColumnCount(); column ++){
            int  width = getParent().getWidth();
            for (int row = 0; row < getRowCount(); row++) {
                TableCellRenderer renderer = getCellRenderer(row, column);
                Component comp = prepareRenderer(renderer, row, column);
                width = Math.max (comp.getPreferredSize().width, width);
            }
            TableColumn col = new TableColumn();
            col = getColumnModel().getColumn(column);
            col.setPreferredWidth(width);
        }
        super.doLayout();
    }
}
Lydie answered 30/11, 2023 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.