Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i get the values of multiple selection from a JTable

Can someone please provide a sample code or at least a method that I can use to get the string values of multiple selections in a JTable? I searched the web, but I found only examples of how to get values from a single selection. Based on that I tried to implement the code myself using loops, but it blew up on my face. Any help will be greatly appreciated.

like image 528
rafaelzm2000 Avatar asked Oct 04 '12 17:10

rafaelzm2000


People also ask

How do you select multiple rows in Java?

It is possible to select an entire row by clicking on its header. It is possible to select multiple rows by Ctrl -clicks on row headers.

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?

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

What is JTable TableModel?

The TableModel interface specifies the methods the JTable will use to interrogate a tabular data model. The JTable can be set up to display any data model which implements the TableModel interface with a couple of lines of code: TableModel myData = new MyTableModel(); JTable table = new JTable(myData);


1 Answers

JTable has a method for that :

    int[] selection = table.getSelectedRows();

Of course, this method returns the indices of the selected rows. You can use these indices to get the values you want from the the table model.

like image 168
Razvan Avatar answered Nov 15 '22 04:11

Razvan