Updated July 4, 2023
Introduction to JComboBox in Java
JComboBox belongs to the Java Swing package. It extends the JComponent class. JComboBox is represented by a popup menu that contains the list of elements and the user could select an option or element from that list. It can be editable or not depending upon the need and the programmer. By default, it is not editable combining the features of a button and a drop-down list. The JComboBox which is not editable has features of the text field and a drop-down list. Users can type or can click on the arrow button to view the drop-down list. Combo Boxes require less space and hence very useful when size is small or limited.
Syntax:
Let us see the syntax of declaring the javax.swing.JComboBox class.
public class JComboBox extends JComponent implements ItemSelectable, ListDataListener, ActionListener, Accessible
JComboBox Constructors
The commonly used constructors are mentioned below:
- JComboBox(): It creates a new empty JComboBox with the default data model.
- JComboBox(Object[] items): It creates a new JComboBox with the elements listed in the specified array.
- JComboBox(Vector<?> items): It creates a new JComboBox with the elements listed in the specified vector.
- JComboBox(ComboBoxModel M): It creates a JComboBox with the elements listed in the specified ComboBoxModel.
JComboBox Methods
Here are some of the methods explained below.
- addItem(Object item): It adds the new item to the JComboBox.
- addItemListener(ItemListener I): It adds an ItemListener to JComboBox.
- getItemAt(int i): It is used to get the item present at the index i.
- getItemCount(): It is used to get the number of items present in the list.
- getSelectedItem(): It is used to get the item that is selected.
- removeItemAt(int i): This is used to remove an item present at an index i.
- setEditable(boolean b): This is used to determine whether the JComboBox can be edited or not.
- setSelectedIndex(int i): It is used to select an item of JComboBox present at index i.
- showPopup(): It causes the JComboBox to display the popup window.
- setUI(ComboBoxUI ui): It is used to set the L&F object that renders the components.
- setSelectedItem(Object a): It is used to set the selected item in the JComboBox display area to the object in an argument.
- setSelectedIndex(int a): It is used to select an item present at index a.
- setPopupVisible(boolean v): It is used to set the visibility of the popup.
- setModel(ComboBoxModel a): It is used to set the data model used by JComboBox to get the list of the items.
- setMaximumRowCount(int count): It is used to set the maximum number of rows a JComboBox can display.
- setEnabled(boolean b): It is used to enable the JComboBox to select the items.
- removeItem(Object anObject): It is used to remove the item from the item list.
- removeAllItems(): It is used to remove all the items present in the item list.
- removeActionListener(ActionListener I): It helps to remove the ActionListener.
- isPopupVisible(): It tells the visibility of the popup.
- addPopupMenuListener(PopupMenuListener I): It is used to add the PopupMenuListener to listen to all the notification messages from the popup.
- getActionCommand(): It is used to return the action command.
- getEditor(): It is used to return the editor the help to edit and paint the selected item in the combo box field.
- getItemCount(): It gives the number of items present in the item list.
- getItemListeners(): It gives the array of all the ItemListeners added to the combo box.
- createDefaultKeySelectionManager(): It gives the instance of the default key selection manager
- fireItemStateChanged(ItemEvent e): It enables all the listeners who are interested in the occurrence of this event to get notified about this event.
- firePopupMenuCanceled(): It is used to notify the listeners of the PopupMenu that the popup of JComboBox has been canceled.
- firePopupMenuWillBecomeInvisisble(): It is used to notify the listeners of the PopupMenu that the popup of JComboBox has made invisible.
- firePopupMenuWillBecomeVisisble(): It is used to notify the listeners of the PopupMenu that the popup of JComboBox has made visible.
- setEditor(ComboBoxEditor a): It is used to set the editor the help to edit and paint the selected item in the combo box field.
- setActionCommand(String a): It is used to set the action command.
- getUI(): It gives the feel and looks object that is related to this component.
- paramString(): It gives the string representation of the combo box.
- getUIClassID(): It gives the name of the feel and looks object that is related to this component.
- getAccessibleContext(): It is used to get the associated context related to the combo box.
Example of JComboBox in Java
A simple program to create a JComboBox and adding elements to it.
Code:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class state extends JFrame implements ItemListener {
static JFrame f;
static JLabel a, b;
static JComboBox c;
public static void main (String [] args)
{
f = new JFrame ("frame");
state st = new state ();
f.setLayout (new FlowLayout());
String str[] = { "Uttar Pradesh", "Orissa", "Gujarat", "Kerala", "Uttaranchal" };
c = new JComboBox (str);
c.addItemListener (st);
a = new JLabel ("select your city ");
b = new JLabel ("Uttar Pradesh is Selected");
a.setForeground (Color.red);
b.setForeground (Color.blue);
JPanel pa = new JPanel ();
pa.add (a);
pa.add (c);
pa.add (b);
f.add (pa);
f.setSize (400, 300);
f.show ();
}
public void itemStateChanged (ItemEvent e)
{
if (e.getSource () == c) {
b.setText (c.getSelectedItem () + “is Selected");
}
}
}
Output:
Conclusion
In this article, we have discussed the creation, constructors, and methods of JComboBox. It is very useful if we have limited space. It is a combination of the text field and drop-down list and occupies less space than list, therefore, more preferred than a list.
Recommended Articles
This is a guide to JComboBox in Java. Here we discuss the basic concept, creation, constructors, and methods of JComboBox with example. You can also go through our other suggested articles –