Java JTable setting Column Width

后端 未结 9 1570
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 09:57

I have a JTable in which I set the column size as follows:

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.getColumnModel().getColumn(0).setPreferredW         


        
9条回答
  •  遥遥无期
    2020-12-02 10:36

    What happens if you call setMinWidth(400) on the last column instead of setPreferredWidth(400)?

    In the JavaDoc for JTable, read the docs for doLayout() very carefully. Here are some choice bits:

    When the method is called as a result of the resizing of an enclosing window, the resizingColumn is null. This means that resizing has taken place "outside" the JTable and the change - or "delta" - should be distributed to all of the columns regardless of this JTable's automatic resize mode.

    This might be why AUTO_RESIZE_LAST_COLUMN didn't help you.

    Note: When a JTable makes adjustments to the widths of the columns it respects their minimum and maximum values absolutely.

    This says that you might want to set Min == Max for all but the last columns, then set Min = Preferred on the last column and either not set Max or set a very large value for Max.

提交回复
热议问题