1. Technology

How to Handle Item Events


Graphical components containing a list of items allows the user to select one or more of those items. For the components that implement the ItemSelectable interface (e.g., JCheckBox, JComboBox, JRadioButton, etc) an item event is triggered when an item is selected or deselected. To listen for an item event the ItemListener interface needs to be implemented.

Note: The JList does not implement the ItemSelectable interface.

The ItemListener Interface

The ItemListener interface processes item events fired by graphical components that offer a list of items to select from. The interface defines one method which must be implemented by a class:

public interface ItemListener {

    public void itemStateChanged(ItemEvent e);

The itemStateChanged method is called when an item has been selected or deselected.

The ItemEvent Object

When an item event is triggered the information about the event is held in an ItemEvent object and passed to the itemStateChanged method. The ItemEvent object contains:

  • the item that was either selected or deselected. The item returned is an object type and will differ depending on the graphical component. For example, a JComboBox would have a String as an item whereas a JCheckBox has a JCheckBox as an item. This makes sense as a JComboBox contains a list of strings to be selected and deselected while the JCheckBox itself is what is being selected and deselected.
  • whether the item was selected or deselected.

Implementing the ItemListener Interface

Let's implement the ItemListener for a JComboBox. The JComboBox can also use the ActionListener event listener to handle items being selected but with the ItemListener we can also keep track of what item was deselected.

Here's a simple graphical user interface with no event handling implemented:

import java.awt.EventQueue;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;

public class ItemListenerExample {

    JTextArea feedback;

    public static void main(String[] args) {
         //Use the event dispatch thread for Swing components
         EventQueue.invokeLater(new Runnable()
             public void run()
                 new ItemListenerExample();         
    public ItemListenerExample()
        JFrame guiFrame = new JFrame();
        //make sure the program exits when the frame closes
        guiFrame.setTitle("Mouse Event Example");
        //This will center the JFrame in the middle of the screen
        //Options for the combo box dialog
        String[] choices = {"Monday", "Tuesday"
                ,"Wednesday", "Thursday", "Friday"};
        JComboBox weekDays = new JComboBox(choices);
        //place ItemListener code here..

        feedback = new JTextArea();
        JScrollPane feedbackScroll = new JScrollPane(feedback);
        guiFrame.add(feedbackScroll, BorderLayout.CENTER);

The application uses a JFrame to hold a JComboBox called weekDays and a JTextArea which will provide feedback on the item events that are triggered. The weekDays combo box is filled with Strings for days of the week. At the moment the application doesn't do anything when it is run so let's implement the ItemListener interface.

First add the import statements to the top of the class:

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

Now, we can implement the ItemListener as an anonymous inner class attached to the weekDays JComboBox using the addItemListener method:

weekDays.addItemListener(new ItemListener(){
   public void itemStateChanged(ItemEvent e)
      String item = (String)e.getItem();
      if (e.getStateChange() == ItemEvent.SELECTED)
          feedback.append(item + " was selected\n");
          feedback.append(item + " was deselected\n");

The itemStateChanged method will be called now that any of the items in the weekDays JComboBox are selected. In fact, it will be called twice - the first time to pass on the information about the item deselected and then about the item selected. As the ItemListener event listener works with different graphical components it can't guarantee the type of object passed by the getItem method so it has to be cast as a String object to retrieve the item value.

When the application is run it will now show what item was deselected and what item was selected in the JTextArea.

The full Java code listing can be found in ItemListener Example Code Program.

  1. About.com
  2. Technology
  3. Java
  4. Create User Interfaces
  5. Handling Events
  6. How to Handle Item Events

©2014 About.com. All rights reserved.