Paranoid Panda Paranoid Panda - 2 months ago 8
Java Question

How to only allow one row at a time to be selected in a JTable?

I was wondering what the best way is to make it so that only one row at a time can be selected in a JTable? Is there a method to set this which I have missed or do I need to override it is some way?

I am using Oracle JDK 8.

Answer

You need to:

table.setRowSelectionAllowed(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);