This section provides a tutorial example on how to use java.awt.event.ItemListener, radio button and check box item listener interface, to catch state changed item vents on radio button menu item objects.
Since JRadioButtonMenuItem class is a special menu item class, it supports another event listener, ItemListenser,
which allows you to catch radio button state changed events:
java.awt.event.ItemListener - An AWT interface that allows you to implement your own radio button state changed event handler methods:
itemStateChanged(ItemEvent) - Event handler method called when the state of the associated button is changed. You need to implement this method to perform your own task.
java.awt.event.ItemEvent - An AWT class that represents an event occurred on a radio button. The most important method in this class is:
getStateChange() - Method returns an integer: 1 if state is changed to selected or 2 if state is changed to deselected.
Here is an example program I wrote to test the ItemListener interface:
/**
* JRadioButtonMenuItemListenerTest.java
* Copyright (c) 2009 by Dr. Herong Yang, http://www.herongyang.com/
*/
import java.awt.event.*;
import javax.swing.*;
public class JRadioButtonMenuItemListenerTest {
JFrame myFrame = null;
public static void main(String[] a) {
(new JRadioButtonMenuItemListenerTest()).test();
}
private void test() {
myFrame = new JFrame("Menu Listener Test");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setBounds(50,50,250,150);
myFrame.setContentPane(new JDesktopPane());
JMenuBar myMenuBar = new JMenuBar();
JMenu myMenu = getFileMenu();
myMenuBar.add(myMenu);
myMenu = getColorMenu();
myMenuBar.add(myMenu);
MyMenuItem myItem = new MyMenuItem("Help");
myMenuBar.add(myItem);
myFrame.setJMenuBar(myMenuBar);
myFrame.setVisible(true);
}
private JMenu getFileMenu() {
JMenu myMenu = new JMenu("File");
MyMenuItem myItem = new MyMenuItem("Open");
myMenu.add(myItem);
myItem = new MyMenuItem("Close");
myMenu.add(myItem);
myMenu.addSeparator();
myItem = new MyMenuItem("Exit");
myMenu.add(myItem);
return myMenu;
}
private JMenu getColorMenu() {
JMenu myMenu = new JMenu("Color");
ButtonGroup myGroup = new ButtonGroup();
MyRadioButtonMenuItem myItem
= new MyRadioButtonMenuItem("Red");
myItem.setSelected(true);
myGroup.add(myItem);
myMenu.add(myItem);
myItem = new MyRadioButtonMenuItem("Green");
myGroup.add(myItem);
myMenu.add(myItem);
myItem = new MyRadioButtonMenuItem("Blue");
myGroup.add(myItem);
myMenu.add(myItem);
return myMenu;
}
private class MyMenuItem extends JMenuItem
implements ActionListener {
public MyMenuItem(String text) {
super(text);
addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Item clicked: "+e.getActionCommand());
}
}
private class MyRadioButtonMenuItem extends JRadioButtonMenuItem
implements ActionListener, ItemListener {
public MyRadioButtonMenuItem(String text) {
super(text);
addActionListener(this);
addItemListener(this);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Item clicked: "+e.getActionCommand());
}
public void itemStateChanged(ItemEvent e) {
System.out.println("State changed: "+e.getStateChange()
+" on "+((MyRadioButtonMenuItem) e.getItem()).getText());
}
}
}
If you run this example, you will see the frame window shows up with the menu bar like this:
If you click the "Help" menu item and click menu items in the "Color" menu,
you will see some messages printed on the Java console window:
State changed: 1 on Red
Item clicked: Help
Item clicked: Red
Item clicked: Red
State changed: 2 on Red
State changed: 1 on Green
Item clicked: Green
State changed: 2 on Green
State changed: 1 on Blue
Item clicked: Blue
Interesting notes about this tutorial example:
I used inner class MyRadioButtonMenuItem to create radio button menu items with both
ActionListener and ItemListener interfaces implemented in the inner class.
"e.getActionCommand()" expression is used to get action command string, the menu item button text in this case.
"e.getStateChange()" expression is used to get state changed signal
Clicking on a selected radio button does not trigger any item events.
Sample programs listed in this section have been tested with JDK 1.6.0.