Question

I have a JTable consisting of multiple rows and columns. I want to make the cells selectable, but only in one row at a time. So for example, when I click on the cell in third row and the fifth column, I can pull the mouse to the left or right and select more cells, but only in this specific row and not in the row above or below.

How can I do this?

Was it helpful?

Solution

Use the setSelectionMode() method from ListSelectionModel interface, and set the selection mode to ListSelectionModel.SINGLE_SELECTION.

This will configure JTable to work with one row at a time selection, blocking selection of multiple rows.

To select single cells, combine the above with setColumnSelectionAllowed(true) on TableColumnModel, and you should get what you need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top