Select/unselect multiple rows in a table

245 Views Asked by At

Java 8, Eclipse 2018-12, WindowsBuilder 1.9.1

I added a JTable with 2 columns and 5 rows to my Frame:

JTable table = new JTable();
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setModel(new DefaultTableModel(
    new Object[][] {
        {null, null},
        {null, null},
        {null, null},
        {null, null},
        {null, null},
    },
    new String[] {
        "ColumnA", "ColumnB"
    }
));

With this code a single row is selected by clicking on it but to select multiple rows you have to press Shift/Ctrl. If you click on another row without pressing Shift/Ctrl, the current selection is deleted.

How do I make it work more like "on/off" (without pressing any additional keys)?

  • Clicking on row 1 selects it
  • Clicking on row 3 selects it, additional to row 1
  • Clicking on row 1 again deselects it (but leaves row 3 selected)

Is this even possible with a JTable? If not, is there anything else that can be used similar to a JTable but can be selected the way I need it to?

0

There are 0 best solutions below