I have a JTable and I need to remove a row, namely, the selected row.
So first, I get the the table model :
DefaultTableModel model = (DefaultTableModel) table.getModel();
Then the selected row (if the second row is selected, this returns 1, which is understandable because rows start from zero):
int selectedRow = table.getSelectedRow();
Then I try to remove the row:
model.removeRow(selectedRow);
Then I set the table model again:
table.setModel(model);
What this achieves is removing a completely random row. I simply can't understand why. I have sorted the table at some point, using table.setRowSorter(sorter), but I don't know why that should be a problem. If an SSCCE is absolutely needed please let me know, because I have a lot of code to modify before I can produce one.
NOTE: The values returned by these two lines differ:
System.out.println(table.getValueAt(selectedRow, 1));
System.out.println(model.getValueAt(selectedRow, 1));
See Question&Answers more detail:os