Java Swing | JComboBox with examples
JComboBox is a part of Java Swing package. JComboBox inherits JComponent class . JComboBox shows a popup menu that shows a list and the user can select a option from that specified list . JComboBox can be editable or read- only depending on the choice of the programmer .
Constructor of the JComboBox are:
- JComboBox() : creates a new empty JComboBox .
- JComboBox(ComboBoxModel M) : creates a new JComboBox with items from specified ComboBoxModel
- JComboBox(E [ ] i) : creates a new JComboBox with items from specified array.
- JComboBox(Vector items) : creates a new JComboBox with items from the specified vector
Commonly used Methods are :
- addItem(E item) : adds the item to the JComboBox
- addItemListener( ItemListener l) : adds a ItemListener to JComboBox
- getItemAt(int i) : returns the item at index i
- getItemCount(): returns the number of items from the list
- getSelectedItem() : returns the item which is selected
- removeItemAt(int i) : removes the element at index i
- setEditable(boolean b) : the boolean b determines whether the combo box is editable or not .If true is passed then the combo box is editable or vice versa.
- setSelectedIndex(int i): selects the element of JComboBox at index i.
- showPopup() :causes the combo box to display its popup window.
- setUI(ComboBoxUI ui): sets the L&F object that renders this component.
- setSelectedItem(Object a): sets the selected item in the combo box display area to the object in the argument.
- setSelectedIndex(int a): selects the item at index anIndex.
- setPopupVisible(boolean v): sets the visibility of the popup.
- setModel(ComboBoxModel a) : sets the data model that the JComboBox uses to obtain the list of items.
- setMaximumRowCount(int count): sets the maximum number of rows the JComboBox displays.
- setEnabled(boolean b): enables the combo box so that items can be selected.
- removeItem(Object anObject) : removes an item from the item list.
- removeAllItems(): removes all items from the item list.
- removeActionListener(ActionListener l): removes an ActionListener.
- isPopupVisible() : determines the visibility of the popup.
- addPopupMenuListener(PopupMenuListener l) : adds a PopupMenu listener which will listen to notification messages from the popup portion of the combo box.
- getActionCommand() : returns the action command that is included in the event sent to action listeners.
- getEditor(): returns the editor used to paint and edit the selected item in the JComboBox field.
- getItemCount() : returns the number of items in the list.
- getItemListeners(): returns an array of all the ItemListeners added to this JComboBox with addItemListener().
- createDefaultKeySelectionManager() : returns an instance of the default key-selection manager.
- fireItemStateChanged(ItemEvent e) : notifies all listeners that have registered interest for notification on this event type.
- firePopupMenuCanceled() : notifies PopupMenuListeners that the popup portion of the combo box has been canceled.
- firePopupMenuWillBecomeInvisible() : notifies PopupMenuListeners that the popup portion of the combo box has become invisible.
- firePopupMenuWillBecomeVisible() : notifies PopupMenuListeners that the popup portion of the combo box will become visible.
- setEditor(ComboBoxEditor a): sets the editor used to paint and edit the selected item in the JComboBox field.
- setActionCommand(String a) : sets the action command that should be included in the event sent to actionListeners.
- getUI() : returns the look and feel object that renders this component.
- paramString() : returns a string representation of this JComboBox.
- getUIClassID() : returns the name of the Look and feel class that renders this component.
- getAccessibleContext() : gets the AccessibleContext associated with this JComboBox
The following programs will illustrate the use of JComboBox
1. Program to create a simple JComboBox and add elements to it .