alves
alves

Reputation: 1343

How to programmatically deselect the currently selected row in a JTable (swing)?

I want to programmatically deselect the currently selected row (or rows) in a JTable.

Basically I want the opposite of this:

JTable table = ...;
table.setRowSelectionInterval(x,x);

I tried (with little hope) using:

table.setRowSelectionInterval(-1,-1)

or

table.setRowSelectionInterval(1,0)

but it doesn't work.

Upvotes: 29

Views: 48037

Answers (2)

Avrom
Avrom

Reputation: 5027

There is a method on JTable called clearSelection. This, in turn calls clearSelection on the ListSelectionModel of the table and the column model.

Upvotes: 69

Steve Landey
Steve Landey

Reputation: 2609

I believe you can use this:

table.getSelectionModel().clearSelection().

The SelectionModel is what actually handles the selection. JTable just has a few shortcuts.

Upvotes: 31

Related Questions