I have a problem with my JTable. My JTable displays content of a database. One database table has the name category. Every category is displayed in the JComboBox. If I click on
Try below code after you change the data in jtable.
model.fireTableDataChanged(); table.repaint();
where model is the tablemodel object and table is JTable object
I hope it helps!!!
The code is buggy because every time actionPerformed()
is called, you are creating a new component:
table = new JTable(new TestTableModel(data, header));
frame.add(new JScrollPane( table )); // <-- BTW: here you need to put the table otherwise you are adding an empty JScrollPane
frame.validate();
(Note: There is an additional bug, @mKorbel mentioned it).
However you have already added a JScrollPane
with a JTable
to the frame and those continue to exist. (if you try to resize the window you will see the new table beneath the old one).
The correct way to update the table data is to get its TableModel
make any modifications required in the model and then depending on what you changed you will fire the apporpriate fireXXX()
method to notify the table to redraw itself.
As a crude example your code would be:
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == this.combobox) {
JComboBox<String> combobox = this.combobox;
newdata.clear(); // Clear your list or create a new one otherwise data will keep piling up.
newdata.add("Test1");
newdata.add("Test2");
TestTableModel model = (TestTableModel) table.getModel();
// Since you need to replace the whole data create a new Object[][] each time
Object[][] newModelData = new Object[newdata.size()][3];
// Set whatever data to the new array
int i = 0;
for (String text : newdata) {
newModelData[i][0] = Boolean.TRUE;
newModelData[i][1] = text;
newModelData[i][2] = text;
i++;
}
// replace all data of the current model
model.setData(newModelData);
}
}
....
// Now inside your table model:
...
@Override
public Class<?> getColumnClass(int column) {
// if (column == 0) {
// return Boolean.class; // <-- this produces a ClassCastException with the data you aretrying to set
// }
return super.getColumnClass(column);
}
public void setData(Object[][] data) {
this.data = data; // <-- Update the data
fireTableDataChanged(); // <-- fire the event so the table is notified. If you change only one cell you need to call the appropriate fire event
}
...
Update 1: Your problem with your new code has fixed the way that you update the data in the model. However you have a logical flaw when you update the data
structure. This variable starts as an array of 3 rows. In the actionPerformed()
method you perform a loop for the length of newdata
list which only has 2 entries. So you update only the first 2 rows of your model.
Update 2: It seems that you are missing the point. How you update your model matters here. The table will display whatever data your model has. If you only update 2 rows but leave the 3rd one unmodified then the table will display 3 rows (2 new ones and an old one). Since you need to change each time all the data then you need to completely replace the data in the model. It is your data that need to be re-created each time not the table. See the update code example. I have added the actionPerformed()
method that re-initializes the data with your current source code. Please read the inline comments.