This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTables is not refreshing its data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTables is not refreshing its data" Watch "JTables is not refreshing its data" New topic

JTables is not refreshing its data

olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148

i already googled and search this forum but i didnt find anything what helps me...

i have a application which has some problems with its jtable (the table headers are not updated, columns cant be removed etc.)
so i created a little app to see if this always occurs:

i use netbeans which uses some default data (column headers)
when i hit jbutton1 it adds a column called "blah" and when i hit multiple times jbutton2 the columns get deleted
but when i hit the jbutton1 again all columns are back again and i dont understand why

can someone tell me whats wrong?
Rob Spoor

Joined: Oct 27, 2005
Posts: 20078

When you call setModel the column model is rebuilt. But why are you calling setModel? The call to DefaultTableModel's addRow method should already add a new row to the GUI automatically - it notifies the JTable.

How To Ask Questions How To Answer Questions
olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
i thought i have to retrieve the model, modify it and set it back (but getModel doesnt return a copy, it returns a reference)
thats clear now, thanks

when i hit jbutton1 it adds a new column, then i click on the jbutton2 until theres only one column left and (thats the problem) now i click the jbutton1 again
now i have all columns back and thats not what i expected
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2373
You don't understand Model-View-Controller (MVC) design of Swing.

Data is stored in a Model (the TableModel). When you add the Model to the View (JTable), the table creates its own TableColumnModel based on the TableModel. You can then customize the TableColumnModel independent of the TableModel. Removing a column from the TableColumnModel does not remove the column from the TableModel. This allows you to share the same TableModel with different views for each JTable.

So removing columns from the TableColumnModel does not affect the TableModel in any way, it only affects the View.

When you then use the setModel() method on the JTable, a completely new TableColumnModel is recreated based on the original TableModel.
olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
ok thanks, i thought the tablemodel and the columnmodel are two independent models i can modify
i'm using now setDataVector
I agree. Here's the link:
subject: JTables is not refreshing its data
It's not a secret anymore!