arrays - How would I remove a specific item from a combo box in Java? -
i'm using string array populate combo box items. after item has been selected , submitted on button press want remove item combo box. attempt remove selected item string array first, remove items combo box , repopulate string array.
choice string array, cbochoice combobox, strchoice item getting removed
for(int = 0; < choice.length; i++) { if(choice[i].equals(strchoice)) { choice[i] = null; cbochoice.removeallitems(); cbochoice.additem(choice); } }
this far i've got, don't know if there simpler method of doing can't seem working.
since have string array , jcombobox have same items in same order, can use jcombobox.getselectedindex()
retrieve index location of selected item , remove jcombobox , you're array.
as suggestion, make string array arraylist, it's "smarter" dynamic array , can stay better in synch jcombobox. make sure remove array first, before removing jcombobox, otherwise selected index change.
an arraylist declaration this:
arraylist<string> choice = new arraylist<>();
add content list so:
choice.add(yourchoice);
removing items followed:
if (cbochoice.getselectedindex() > -1) { choice.remove(cbochoice.getselectedindex()); cbochoice.getselectedindex(); }
hope helps... also, once understand how works, suggest studying comboboxmodel. swing controls have model objects can use add/remove/modify contents without having reference actual control.
Comments
Post a Comment