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
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.
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
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.
Joined: Jun 20, 2009
ok thanks, i thought the tablemodel and the columnmodel are two independent models i can modify
i'm using now setDataVector