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.
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.
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 .
So you can call table. getSelectionModel(). isSelectionEmpty() to find out if any row is selected.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With