Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTables is not refreshing its data

 
olze oli
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

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
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
olze oli
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

but:
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
Posts: 2616
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks, i thought the tablemodel and the columnmodel are two independent models i can modify
i'm using now setDataVector
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic