Item Listener on Radio Button Menu Items

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, ItemListener, 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:

java.awt.event.ItemEvent - An AWT class that represents an event occurred on a radio button. The most important method in this class is:

Here is an example program I wrote to test the ItemListener interface:

 * Copyright (c) 1997-2024 All Rights Reserved.
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.setContentPane(new JDesktopPane());

      JMenuBar myMenuBar = new JMenuBar();
      JMenu myMenu = getFileMenu();
      myMenu = getColorMenu();

      MyMenuItem myItem = new MyMenuItem("Help");

   private JMenu getFileMenu() {
      JMenu myMenu = new JMenu("File");
      MyMenuItem myItem = new MyMenuItem("Open");
      myItem = new MyMenuItem("Close");
      myItem = new MyMenuItem("Exit");
      return myMenu;
   private JMenu getColorMenu() {
      JMenu myMenu = new JMenu("Color");
      ButtonGroup myGroup = new ButtonGroup();
      MyRadioButtonMenuItem myItem 
         = new MyRadioButtonMenuItem("Red");
      myItem = new MyRadioButtonMenuItem("Green");
      myItem = new MyRadioButtonMenuItem("Blue");
      return myMenu;
   private class MyMenuItem extends JMenuItem 
      implements ActionListener {
      public MyMenuItem(String text) {
      public void actionPerformed(ActionEvent e) {
         System.out.println("Item clicked: "+e.getActionCommand());
   private class MyRadioButtonMenuItem extends JRadioButtonMenuItem
      implements ActionListener, ItemListener {
      public MyRadioButtonMenuItem(String text) {
      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:

Radio Button Menu Item Listener Test
Radio Button Menu Item Listener Test

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:

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

Menu Bar, Menus, Menu Items and Listeners

 JMenuBar, JMenu, and JMenuItem Classes - Menu Bar Test Program - Menu Test Program - Menu Item Test Program - Radio Button Menu Item Test Program - Check Box Menu Item Test Program

 javax.swing.event.MenuListener - Menu Listener Interface - Menu Item Action Listener Test

Item Listener on Radio Button Menu Items

 Item Listener on Check Box Menu Items

 javax.swing.event.MenuKeyListener - Menu Key Listener Interface

 setMnemonic() - Setting Keyboard Mnemonics on Menu Items

 setAccelerator() - Setting Keyboard Accelerators on Menu Items

 setMnemonic() - Setting Keyboard Mnemonics on Menus

 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


 Full Version in PDF/EPUB