java - Swing: JComboBox removing/adding duplicates -


my problem has able add , remove items combobox. items can duplicates. manage add them:

jcombobox1.additem(dodatek); 

remove first:

 if (jcombobox1.getitemcount() > 0) {                 jcombobox1.removeitemat(0); 

or selected one:

    if (jcombobox1.getselecteditem() != null) {         jcombobox1.removeitemat(jcombobox1.getselectedindex()); 

problem occurs when having items removed, , 1 of them duplicate. in case last item remains selected in combobox though list empty! http://imgur.com/smf2hwg

it throws exception when trying remove selected item:

exception in thread "awt-eventqueue-0" java.lang.arrayindexoutofboundsexception: array index out of range: -1     @ java.util.vector.removeelementat(vector.java:562)     @ javax.swing.defaultcomboboxmodel.removeelementat(defaultcomboboxmodel.java:152)     @ javax.swing.jcombobox.removeitemat(jcombobox.java:759) 

this happens when removing items among there duplicates

selected item , selected index don't mean same thing. need check, if selected index != -1 before calling remove on it.

selected item value selected in box, selected index selected index in list.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -