Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting selected row through AbstractTableModel

Tags:

java

swing

jtable

Is it possible to get the selected row index from my table model?

My object already knows about the table model. Instead of passing a reference to the table it self can i get the selected index using the model?

like image 432
Hamza Yerlikaya Avatar asked May 13 '09 09:05

Hamza Yerlikaya


2 Answers

Like MrWiggles said you can get it from the ListSelectionModel which you is accessible from the table itself. However there are convenience methods in JTable to get the selected rows as well. If your table is sortable etc you will also need to go through the convertRowIndexToModel method :)

From the JTable JavaDoc:

   int[] selection = table.getSelectedRows();
   for (int i = 0; i < selection.length; i++) {
     selection[i] = table.convertRowIndexToModel(selection[i]);
   }
   // selection is now in terms of the underlying TableModel
like image 152
willcodejavaforfood Avatar answered Oct 15 '22 23:10

willcodejavaforfood


The TableModel only concerns itself with the data, the ListSelectionModel concerns itself with what is currently selected, so, no you can't get the selected row from the TableModel.

like image 29
tddmonkey Avatar answered Oct 16 '22 00:10

tddmonkey