Java Swing Tutorials - Herong's Tutorial Examples - v4.32, by Herong Yang
ActionListener, ChangeListener and ItemListener
This section provides a tutorial example on how to use ActionListener, ChangeListener and ItemListener interfaces to handle different types of events generated on check boxes.
As you can see from the previous section, a check box can have 3 types of event listeners: ActionListener, ChangeListener, and ItemListener. The following sample program shows you when those listeners are called, and how many times:
/* JCheckBoxTest.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 JCheckBoxTest { public static void main(String[] a) { JFrame f = new JFrame("My Check Boxes"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyCheckBox red = new MyCheckBox("Red"); f.getContentPane().add(red,BorderLayout.NORTH); MyCheckBox green = new MyCheckBox("Green"); f.getContentPane().add(green,BorderLayout.CENTER); MyCheckBox blue = new MyCheckBox("Blue"); f.getContentPane().add(blue,BorderLayout.SOUTH); f.pack(); f.setVisible(true); } private static class MyCheckBox extends JCheckBox implements ActionListener, ChangeListener, ItemListener { static int count = 0; String text = null; public MyCheckBox(String t) { super(t); text = t; addActionListener(this); addChangeListener(this); addItemListener(this); } public void actionPerformed(ActionEvent e) { count++; System.out.println(count+": Action performed - "+text); } public void stateChanged(ChangeEvent e) { count++; System.out.println(count+": State changed - "+text); } public void itemStateChanged(ItemEvent e) { count++; System.out.println(count+": Item state changed - "+text); } } }
This example program creates 3 check boxes. Each button has 3 listeners to handle 3 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 3 check boxes: "Red", "Green" and "Blue":
If you move your mouse to the "Red" check box, you see 1 "State changed" message. If you press the "Red" check box, you see 2 more "State changed" messages. If you release the "Red" check box, you see 5 more messages. If you move your mouse away from the "Red" check box, you see 1 more "State changed" message.
1: State changed - Red 2: State changed - Red 3: State changed - Red 4: State changed on - Red 5: Item state changed - Red 6: State changed on - Red 7: Action performed - Red 8: State changed on - Red 9: State changed on - Red 10: State changed on - Green 11: State changed on - Green 12: State changed on - Green 13: State changed on - Green 14: Item state changed - Green 15: State changed on - Green 16: Action performed - Green 17: State changed on - Green 18: State changed on - Green
Note that:
Table of Contents
Introduction of Java Swing Package
Graphics Environment of the Local System
►JCheckBox - Swing Check Box Class
javax.swing.JCheckBox and Related Methods
►ActionListener, ChangeListener and ItemListener
isSelected() - Getting State of Check Box
JRadioButton - Swing Radio Button Class
JTextField - Swing Text Field Class
JComboBox - Swing Combo Box Class
Menu Bar, Menus, Menu Items and Listeners
Creating Internal Frames inside the Main Frame
Layout of Components in a Container
JEditorPane - The Editor Pane Class
SwingWorker - The Background Task Worker
AWT (Abstract Windows Toolkit)