1

So I have a JTable with 24 columns. When the table is initialized, I make set the widths of the columns and the user interface looks great. No complaints. The user can click a button to only see 4 of the columns. I change the array of columns on the table model and then call

firetablestructurechanged()

which then removes all of the column widths I established (but now only shows the 4 columns, the intended behavior). When I try re-defining the column widths, nothing seems to change.

This is what I use to set column widths

TableColumnModel columnModel = table.getColumnModel();
for (int i = 1; i < columnModel.getColumnCount(); i++) {
columnModel.getColumn(i).setMinWidth(50);
columnModel.getColumn(i).setMaxWidth(50);
}

How do I get the columns to resize after firetablestructurechanged() gets called?

Jamie McIlroy
  • 459
  • 2
  • 8
  • 19
  • 1
    One approach is to provide a customised `TableColumnModel` which can "hide" and "show" columns. [This example](http://www.stephenkelvin.de/XTableColumnModel/) is a little out of date, but the basic premise is sound... – MadProgrammer Nov 13 '14 at 21:20
  • Just a suggestion, could you use [JXTable](http://stackoverflow.com/a/22417960/1795530) to allow users toggle columns? I think your problem will be easily solved and user experience highly improved not only for this feature but others added in that library (i.e.: the ability to search in a table), not to mention that columns still can be resizable and you don't have to waste time dealing with fixed sizes. – dic19 Nov 13 '14 at 21:29
  • 1
    [This answer](http://stackoverflow.com/a/10089138/524900) shows how to hide columns of a JTable. No need for a custom `ColumnTableModel`. All you need to do is remove the columns from the default `TableColumnModel`, this does _not_ remove them from the `TableModel`, thus you shouldn't need to call `fireTableStructureChanged()`. – Lolo Nov 13 '14 at 21:31

0 Answers0