java - removing multiple selections from JList -
i have jlist elements multiple selection allowed. before these elements added jlist, information them being stored in static hashmap in separate class. when more 1 items selected , 'remove selected' button pressed, trying remove selected items (which works fine) , delete records hashmap. reason though, if select more 1 elements, first record in hashmap removed. don't understand how works jlist doesn't work hashmap. code below:
remove.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { object[] selectedelementsvalues = jlist.getselectedvalues(); (int = 0; < selectedelementsvalues.length; i++) { system.out.println(jlist.getselectedvalue().tostring()); system.out.println(personclass.map.get(jlist.getselectedvalue().tostring())); personclass.map.remove(jlist.getselectedvalue().tostring()); system.out.println(personclass.map); }
it works fine if select 1 item @ time , remove it. not multiple selection. items jlist removed properly, though, don't see why doesn't same map.
thx
the problem loop removes items map uses jlist.getselectedvalue().tostring()
, when jlist
selection not modified. can use selection array obtained earlier:
for (object o : selectedvalues) { personclass.map.remove(o.tostring()); }
note getselectedvalues()
deprecated, , should use getselectedvalueslist()
instead.
Comments
Post a Comment