How to set or change JTable column width?
Each column of a
JTable component is represented by the
TableColumn class. The method for setting or changing the width of the column includes the
setPreferredWidth(). These methods is used to set the minimum, maximum and the preferred width of the column respectively.
When we set only the preferred width of a table column and the container get resized the preferred width will be used to recalculate the new column width to fill the available space, but the preferred width value itself does not change.
TableColumn object of a table can be obtained by calling table's
getColumnModel() method which return an instance of
TableColumnModel. After having the
TableColumModel in hand we can get the table's column by calling the
getColumn(int index) method and passes the
index of the column.