Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting multiple rows of JTable

Tags:

java

swing

jtable

I'm currently using JTable to show the contents in database. I want to provide the facility for user so that he can select the number of rows he wants using shift+arrow key and then later on delete those records using the option provided for deletion. Please provide a small example.

like image 281
Supereme Avatar asked Jan 26 '10 17:01

Supereme


People also ask

How can count number of rows in JTable?

You can use the getRowCount() method: Returns the number of rows that can be shown in the JTable , given unlimited space. If a RowSorter with a filter has been specified, the number of rows returned may differ from that of the underlying TableModel .

How can I tell if a row is selected in JTable?

JTable has a getSelectionModel() method which will give you a ListSelectionModel object. It tells you what rows are selected. You can add a ListSelectionListener to that via the addL..S..


1 Answers

You need to allow multiple selection:

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

Then you need to write appropriate selection listener. It's a bit harder, try to find in google related solutions. You can look at an example of selection listener.

like image 104
Roman Avatar answered Sep 26 '22 18:09

Roman