(JTable) AbstractTableModel.fireTableStructureChanged() causes the JTable to shrink its width and move all contents to the left side of the screen, leaving ugly whitespace in the table.

Joseph Shelby

This is covered by bug number 4193727 at the JDC Bug Parade has been fixed in "Kestrel", aka JDK 1.3. however, its still a pain right now.

The workaround  

The bug is AbstractTableModel.fireTableStructureChanged(). it will cause the table columns to shrink off to the left and each be about 150 pixels wide. no attempt to setSize() on the table will change that. nor will a standard "invalidate/validate" attempt either.

The fix  

Reset the size of the wrapping JScrollPane first, then invalidate/validate the JScrollPane (not the table). It will cause a "flicker", but its better than nothing; also means that you'll need to keep a reference to the table's JScrollPane somewhere as well.

// with JScrollPane jsp = new JScrollPane(myTable);
jsp.setSize(jsp.getWidth() - 1, jsp.getHeight());

You can call this code either in an additional TableModelListener, or call it immediately after you call the code that changes the table model, provided you have access to the model involved. If in a listener, the event should have

getFirstRow() == HEADER_ROW; 

that way, you don't call this when its not appropriate.