i wanted see element getting selected, , change other labels , texfields on frame per index. code follows:
list = new jlist(listmodel); list.setselectionmode(listselectionmodel.single_selection); list.setlayoutorientation(jlist.vertical); list.addlistselectionlistener(new listselectionlistener() { public void valuechanged(listselectionevent e) { system.out.println(e.getlastindex()); } });
when clicked first element output: 0 0
after clicking second element: 1 1
after tried click first element again, time output 1 1
again. when tried 25 elements, selecting last element , after click first element , output 23 23
. event's problem or it's code?
the behaviour standard one, if want have different one, create own selectionlistener
considers getvalueisadjusting().
class sharedlistselectionhandler implements listselectionlistener { public void valuechanged(listselectionevent e) { listselectionmodel lsm = (listselectionmodel)e.getsource(); int firstindex = e.getfirstindex(); int lastindex = e.getlastindex(); boolean isadjusting = e.getvalueisadjusting(); output.append("event indexes " + firstindex + " - " + lastindex + "; isadjusting " + isadjusting + "; selected indexes:"); if (lsm.isselectionempty()) { output.append(" <none>"); } else { // find out indexes selected. int minindex = lsm.getminselectionindex(); int maxindex = lsm.getmaxselectionindex(); (int = minindex; <= maxindex; i++) { if (lsm.isselectedindex(i)) { output.append(" " + i); } } } output.append(newline); } }
Comments
Post a Comment