Javlonbek Sharipov
Javlonbek Sharipov

Reputation: 5

Removing selected JTable elements

Several items in the table should be selected and erased when the button is pressed on ArrayList. But only one item is being deleted.

for (int i = 0; i < Table.getRowCount(); i++) {
            if (Table.isRowSelected(i)) {
               TableData.remove(i);
            }
}
Table.setModel(new DemoTableModel(TableData));

Upvotes: 0

Views: 323

Answers (1)

camickr
camickr

Reputation: 324098

You should NOT be removing data from the ArrayList.

The ArrayList can be used to load data into the DefaultTableModel but after you add the TableModel to the table all changes to the data should be done via the DefaultTableModel.

So in your case you would use:

model.removeRow(...)

method of the DefaultTableModel.

See: How to delete multiple rows from JTable , database at a time for a working example that deletes all selected rows from the DefaultTableModel.

If you are using a custom TableModel, then the custom model should implement a removeRow(...) method. See Row Table Model for a step-by-step example that creates a custom TableModel using an ArrayList to hold the data. It show how to implement the "remove row" method.

Upvotes: 2

Related Questions