aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes changing headers on JTable caused other problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "changing headers on JTable caused other problems" Watch "changing headers on JTable caused other problems" New topic
Author

changing headers on JTable caused other problems

Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 236
Hi Guys,

I'm working with a JTable which sets up certain columns with cell renderers and editors. My application allows the user to change the interface to a different language, so JMenuItems, buttons, etc. change their text and I've set it up so that the header on the JTable changes as well. Two lines of code make that happen:



All this part works as expected, however, after the call to fireTableStructureChanged() has completed, the columns that were governed by cell renderers and editors are no longer in effect. Those cells have reverted back to working as normal JLabel cells. What happened? How do I prevent losing the renderers and editors on those columns?

See below for additional code, which might help.

Alan

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2097
    
    7
the columns that were governed by cell renderers and editors are no longer in effect. Those cells have reverted back to working as normal JLabel cells.


When the fireTableStructureChaged() event occurs, the TableColumnModel is rebuilt, which reacreates all the TableColumns, so you lose the renderers/editors/ that you previously added.

How do I prevent losing the renderers and editors on those columns?


Don't invoke the fireTableStructureChanged() method. Instead update your TableColumns of the TableColumnManger manually. You can use the setHeaderValue() method to update the header text. You will then need to invoke repaint() on the table header.
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 236
Thanks, I'll give that a try.

Alan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: changing headers on JTable caused other problems
 
Similar Threads
Why I can't catch NumberFormatException in the tableModel
JTables & Combo Boxes
Right Align numbers in Jtable
Problem in adding/deleting rows from Jtable
adding a row to a jtable by pressing a key