user3758262
user3758262

Reputation: 21

Remove List elements selected in JTable

I have a JTable based on table model that uses a List, I would like to delete elements that are selected in this JTable, from the List, the JTable is sortable, so when i get selected rows, I can't simply list.remove, because order is different. Any idea to solve this?

Upvotes: 0

Views: 73

Answers (1)

tenorsax
tenorsax

Reputation: 21223

Take a look at JTable methods convertRowIndexToView and convertRowIndexToModel:

The selection is always in terms of JTable so that when using RowSorter you will need to convert using convertRowIndexToView or convertRowIndexToModel. The following shows how to convert coordinates from JTable to that of the underlying model:

  int[] selection = table.getSelectedRows();
  for (int i = 0; i < selection.length; i++) {
    selection[i] = table.convertRowIndexToModel(selection[i]);
  }
  // selection is now in terms of the underlying TableModel

Check out How to Use Tables # Sorting and Filtering for some examples.

Upvotes: 5

Related Questions