jesric1029 jesric1029 - 1 year ago 110
Java Question

JTable if any row is selected

I am trying to figure out how to add logic to a JTable via an "if" statement that checks to see if ANY row is selected. I know how to check if a specific row is selected but I can't seem to figure out how to check all rows.


Obviously that is checking for a specific row.

I've also tried something like


This did not work nor did I expect it to work.

The reason for this is that I'm setting up the table so that when the user clicks a row and then hits an "edit" button a second table will appear with more data related to the row they selected. (Gets complicated here with 2D arrays inside of a hash map but I first need to get by this simple problem).

Thanks for the help in advance!

Answer Source

This should be possible with a ListSelectionModel, which can be retrieved using JTable::getSelectionModel()

So you can call table.getSelectionModel().isSelectionEmpty() to find out if any row is selected.