Auto resizing the JTable column widths

后端 未结 4 863
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 05:13

I need my JTable to automatically re-size its column widths to fit the content. I found the TableColumnAdjuster class very useful. But there\'s a small problem. Say i have 5

4条回答
  •  独厮守ぢ
    2020-11-27 05:56

    There is no option to automatically resize one column larger than the other.

    Maybe you can to something like:

    tca = new TableColumnAdjuster( table, 0 );
    tca.adjustColumns();
    TableColumnModel tcm = table.getColumnModel();  
    TableColumn tc = tcm.getColumn(1);
    tc.setWidth(tc.getWidth() + 25);
    

    This would allow you to add extra space to column 1. This extra space would only be added the first time the table is displayed.

    Another option is to use:

    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    

    This would allocate extra space proportionally to each column.

提交回复
热议问题