Auto resizing the JTable column widths

后端 未结 4 878
伪装坚强ぢ
伪装坚强ぢ 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 06:05

    You can do this:

    JPanel jp = new JPanel();
    jp.add(table);
    
    jp.setLayout(new GridLayout(1,1)); /* little trick ;) and believe me that this step is important to the automatic all columns resize! A import is also needed for using GridLayout*/
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); // this is obvius part
    

提交回复
热议问题