Bagira
Bagira

Reputation: 2243

Invoking getSelectedRow method on does not return consistent results

My JTable has couple of rows listed. For the first time when i invoke getselectedRow() it gives me the correct result but after that it always returns me -1.

After the first call to getSelectedRow() I am deleting the selected row and then I am trying to refresh the table and that's what seems to be causing this issue. If i remove the code for refreshing the table it works fine again.

For refreshing the table, first i invoke

model.getDataVector().removeAllElements();

and after that I rebuild the table as i would for the first time i.e. hitting the database and inserting rows into model.

Any suggestions what I need to do?

Upvotes: 1

Views: 189

Answers (1)

mKorbel
mKorbel

Reputation: 109823

For the first time when i invoke getselectedRow() it gives me the correct result but after that it always returns me -1.

this is possible only in the case that

  • isn't any row selected

  • JTable is empty

you have to test if (table.getSelectedRow() != -1) { before code execution

Upvotes: 3

Related Questions