I've a JTable with a ListSelectionModel and ListSelectionListener.
The selection model is set in the JTables constructor: lsm.getSelectionModel()
and the ListSelectionListener is set via a public method:
public void setListSelectionListener(ListSelectionListener l){
lsm.addListSelectionListener(l);
}
called from the Controller class:
view.setTableSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent e){
if (!e.getValueIsAdjusting()) {
int viewRow = e.getFirstIndex();
System.out.println(viewRow + " is selected");
}
}
});
because the listener is created in another class I can't use the JTable's getSelectedRow(); method, but using the ListSelectionEvent object's getFirstIndex(); obviously doesn't get the current selection.
So I'm now using int viewRow = ((ListSelectionModel)e.getSource()).getLeadSelectionIndex());
Does that seem like the correct way to get the current selection? It seems to be working, but I'm not sure if this is a bad way of doing it. Thanks
Only getMinSelectionIndex() and getMaxSelectionIndex() works, which returns the min and max of selected indices respectively. Lead/anchor selected index may >= 0 even when there is no selected row.