java - ListSelectionModel and listener -
i've jtable listselectionmodel , listselectionlistener.
the selection model set in jtables constructor: lsm.getselectionmodel()
and listselectionlistener set via public method:
public void setlistselectionlistener(listselectionlistener l){ lsm.addlistselectionlistener(l); }
called controller class:
view.settableselectionlistener(new listselectionlistener(){ @override public void valuechanged(listselectionevent e){ if (!e.getvalueisadjusting()) { int viewrow = e.getfirstindex(); system.out.println(viewrow + " selected"); } } });
because listener created in class can't use jtable's getselectedrow();
method, using listselectionevent object's getfirstindex();
doesn't current selection.
so i'm using int viewrow = ((listselectionmodel)e.getsource()).getleadselectionindex());
does seem correct way current selection? seems working, i'm not sure if bad way of doing it. thanks
only getminselectionindex() , getmaxselectionindex() works, returns min , max of selected indices respectively. lead/anchor selected index may >= 0 when there no selected row.
Comments
Post a Comment