ActionListener and ItemListener

This section provides a tutorial example on how to use ActionListener and ItemListener interfaces to handle different types of events generated on combo box.

As you can see from the previous section, a check box can have 2 types of event listeners: ActionListener and ItemListener. The following sample program shows you when those listeners are called, and how many times:

/* JComboBoxTest.java
 * Copyright (c) 1997-2024 HerongYang.com. All Rights Reserved.
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JComboBoxTest {
   public static void main(String[] a) {
      JFrame f = new JFrame("My Combo Box");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
      JLabel color = new JLabel("Colors:");
      f.getContentPane().add(color,BorderLayout.NORTH);
      String[] options = {"Red", "Green", "Blue"};
      MyComboBox list = new MyComboBox(options);
    list.setSelectedIndex(-1);
      f.getContentPane().add(list,BorderLayout.SOUTH);
       
      f.pack();
      f.setVisible(true);
   }
   private static class MyComboBox extends JComboBox<String>
      implements ActionListener, ItemListener  {
      static int count = 0;
      public MyComboBox(String[] l) {
         super(l);
         addActionListener(this);
         addItemListener(this);
         this.setSelectedIndex(-1);
      }
      public void actionPerformed(ActionEvent e) {
         count++;
         System.out.println(count+": Action performed - ");
      }
      public void itemStateChanged(ItemEvent e) {
         count++;
         System.out.println(count+": Item state changed - ");
      }
   }
}

This example program creates a combo box with 3 options. It has 2 listeners to handle 2 different types of events. A counter is used in the listener class to help to identify the order of events.

If you run this program, you will see a combo box with 3 options: "Red", "Green" and "Blue". By default, the first option will be selected. But I called the setSelectedIndex(-1) to removed the selection.

JComboBox Listeners
JComboBox Listeners

If you open the drop-down list, you see messages 1 and 2. If you select the "Red" option, you see messages 3 and 4. If you change the selection to "Green", you see messages 5, 6, and 7.

1: Item state changed -
2: Action performed -

3: Item state changed -
4: Action performed -

5: Item state changed -
6: Item state changed -
7: Action performed -

Note that:

Table of Contents

 About This Book

 JDK (Java Development Kit)

 Introduction of Java Swing Package

 Graphics Environment of the Local System

 JFrame - Main Frame Class

 JLabel - Swing Label Class

 JButton - Swing Button Class

 JCheckBox - Swing Check Box Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

JComboBox - Swing Combo Box Class

 javax.swing.JComboBox and Related Methods

ActionListener and ItemListener

 getSelectedItem() - Selected Item of Combo Box

 setEditable() - Use Combo Box as Text Field

 Menu Bar, Menus, Menu Items and Listeners

 Creating Internal Frames inside the Main Frame

 Layout of Components in a Container

 LookAndFeel and UIManager

 Option Dialog Boxes

 JEditorPane - The Editor Pane Class

 SwingWorker - The Background Task Worker

 AWT (Abstract Windows Toolkit)

 Integration with Desktop System

 Archived Tutorials

 References

 Full Version in PDF/EPUB