Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Swing JComboBox & ItemListener

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:

  1. Normal Combo Box.
  2. Editable Combo Box.

Have a look at the below picture:

JComboBox - Normal and Editable
JComboBox – Normal and Editable

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.

Video 1: JComboBox and ItemListener

2. About the Swing JComboBox Example

Below is the example which we will create here:

Java Swing - JComboBox Example
Java Swing – JComboBox Example

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).

Video 2: About the JComboBox Example

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.

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.

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:

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.

UnderStanding ItemEvent of JComboBox
UnderStanding ItemEvent of JComboBox

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:

7. Youtube: Code Implementation

Video 3: Implementing JConboBox Example

8. Code Reference



Categories: Swing

Tags: , , , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.