JTable selection behaviour

If you need a Java Swing JTable to have the following behaviour:

  • click to select a row
  • click on the same row to deselect
  • never having more than one row selected

.. then add this to your subclassed JTable:

public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend)  {
    if (getSelectedRow() == rowIndex) {
        super.changeSelection(rowIndex, columnIndex, !extend, extend);
    } else {
        super.changeSelection(rowIndex, columnIndex, false, extend);
    }
}

Leave a Reply

Dansette