1. About Swing JComboxBox
The Java Swing JComboBox component is useful for picking a specific value from the list of available value. There are two kinds of Combo box in the Java Swing. They are:
- Normal Combo Box.
- Editable Combo Box.
Have a look at the below picture:
The JComboBox on the left side is a Normal Combo Box type. It comes with a button and a list. In the above picture, the word ‘Three’ is placed on the button part and Java Swing placed five strings on the list part.
The Swing JComboBox on the right side is an Editable Combo Box type. When the item is not present on the List, the user can type their custom choice in the editable part of the JComboBox. In the above depiction, the user typed Eight in the editable part as it is not available on the drop-down list.
1.1 Method addItem
The addItem method will add an object to the combo box. In our example, we will add a unique string to the combo box. If you want to add duplicate items, use the makeObj to create the string and pass it to the Swing JComboBox.
1.2 Method setEditable
Now, we know java supports two kinds of combo box (Normal, Editable). The setEditable method takes a boolean and when we pass true, the combo box turns into editable.
1.3 ItemListener & ItemEvent
Java Swing ComboBox raises ItemEvent, and this can be captured by implementing the ItemListener. ItemListener expects to define the itemStateChanged method which received ItemEvent as param. The getStateChange method of the ItemEvent can tell an item is in selected state or not.
2. About the Swing JComboBox Example
Below is the example which we will create here:
The example contains two JLabels, two JTextFields and a JComboBox. Note, the above screen shows Java Swing JComboBox as an editable type. When the user selects an item in the combo box, we show that in the first text box. We also show the past selected item in the Deselected JTextField. A combo box can show only one item once the drop-down is collapsed. So when the user picks an item, in combo box point of view, one item was selected by the user and one was de-selected (the previous one).
3. Load JComboBox With Strings
One can create an empty JComboBox and then add strings to it. In the below code, we have two JTextField as class data members. Then, from Line 17-23, we add sample strings to the JComboBox.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
@SuppressWarnings("serial") public class JComboBoxExample extends JFrame { //Sample 03: Declare JTextFields JTextField txtSelected; JTextField txtUnSelected; @SuppressWarnings({ "rawtypes", "unchecked" }) public JComboBoxExample(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 350, 130); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create ComboBox and Add to Frame JComboBox jcombo = new JComboBox(); jcombo.addItem("One"); jcombo.addItem("Two"); jcombo.addItem("Three"); jcombo.addItem("Four"); jcombo.addItem("Five"); |
4. Prepare JFrame Window
Our example requires two labels and two text fields. The below code through lines 2-5 creates them. After creating the controls, the code adds them to the content pane of the JFrame by calling the add method.
1 2 3 4 5 6 7 8 9 10 |
//Sample 04: Add Two Text Boxes and Labels txtSelected = new JTextField(20); txtUnSelected = new JTextField(20); JLabel lbl1 = new JLabel("Selected"); JLabel lbl2 = new JLabel("DeSelected"); ControlHost.add(lbl1); ControlHost.add(txtSelected); ControlHost.add(lbl2); ControlHost.add(txtUnSelected); ControlHost.add(jcombo); |
5. Swing JComboBox & ItemListener
As already told, the ItemListener will watch for the ItemEvent produced by the JComboBox. Java Swing Combo box component will raise this event when the user the changes the current selection.
In the below code, at line 6, we check the selected state of the JComboBox item by making use of getStateChange method of the ItemEvent instance passed in as a parameter. At line 13, we call the getItem and this will return the item effected by the ItemEvent. The code is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//Sample 05: Handle Item State Change Event jcombo.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { txtSelected.setText( (String) jcombo.getSelectedItem()); } else { txtUnSelected.setText((String)e.getItem()); } } }); |
Have a look at the below picture. Let us say the current selection is Three and the user is making a new selection, which is Five. Now our event handler itemStateChanged will be called twice because there are two items which undergo the state change. The item Three moves from Selected to Unselected. Whereas Item Five moves from Unselected to Selected state.

Our above handler will be first called for the item three, and it will be called again for the item five. So the Deselected textbox will be filled first and then the selected text box.
6. Editable Swing JComboBox
We can change the JComboBox as Editable by making a call to the setEditable method. In the below code, we turn our JComboBox as editable:
1 2 |
//Sample 06: Make this ComboBox Editable jcombo.setEditable(true); |
7. Youtube: Code Implementation
8. Code Reference
8.1 MainEntry.java
1 2 3 4 5 6 7 8 9 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { //Sample 06: Create Instance of JFrameDemo JComboBoxExample frame = new JComboBoxExample("JComboBox Example - Normal, Editable"); frame.setVisible(true); } } |
8.2 JComboBoxExample.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
package tube.coding.examples; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.HeadlessException; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; @SuppressWarnings("serial") public class JComboBoxExample extends JFrame { //Sample 03: Declare JTextFields JTextField txtSelected; JTextField txtUnSelected; @SuppressWarnings({ "rawtypes", "unchecked" }) public JComboBoxExample(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 350, 130); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create ComboBox and Add to Frame JComboBox jcombo = new JComboBox(); jcombo.addItem("One"); jcombo.addItem("Two"); jcombo.addItem("Three"); jcombo.addItem("Four"); jcombo.addItem("Five"); //ControlHost.add(jcombo); //Sample 04: Add Two Text Boxes and Labels txtSelected = new JTextField(20); txtUnSelected = new JTextField(20); JLabel lbl1 = new JLabel("Selected"); JLabel lbl2 = new JLabel("DeSelected"); ControlHost.add(lbl1); ControlHost.add(txtSelected); ControlHost.add(lbl2); ControlHost.add(txtUnSelected); ControlHost.add(jcombo); //Sample 05: Handle Item State Change Event jcombo.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { txtSelected.setText( (String) jcombo.getSelectedItem()); } else { txtUnSelected.setText((String)e.getItem()); } } }); //Sample 06: Make this ComboBox Editable jcombo.setEditable(true); } } |
Categories: Swing
Tags: Editable JComboBox, getItem(), getStateChange(), ItemEvent, ItemListener, itemStateChanged, Normal JComboBox