Adding and Removing an Item in a JComboBox Component

There is no method for replacing an item. To replace an item, first remove the item and then insert the new one.
// Create a read-only combobox; the combobox is read-only // in that it does not allow the user to type in a new item. // The combobox still allows programmatic changes to its items. String[] items = {"item1", "item2"}; JComboBox cb = new JComboBox(items); // Add an item to the start of the list cb.insertItemAt("item0", 0); // Add an item after the first item cb.insertItemAt("item0.5", 1); // Add an item to the end of the list cb.addItem("item3"); // Remove first item cb.removeItemAt(0); // Remove the last item cb.removeItemAt(cb.getItemCount()-1); // Remove all items cb.removeAllItems();

Comments

12 Feb 2010 - 10:45am by Jonathan (not verified)

Best reference for a ComboBox empty items

29 Mar 2010 - 9:36am by Anonymous (not verified)

thanks for help!!!

18 Jun 2010 - 5:51am by Anonymous (not verified)

hello can any one tell me how to remove the item from JComboBox where that item is a string

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.