MenuItemActionListenerTest.java - Menu Item Action Listener Test

This section provides a tutorial example on how to use java.awt.event.ActionListener, button action listener interface, to catch events fired by JMenuItem objects.

Like any other user interface components, JMenuItem objects also fire events when users interact with them. If you want to perform a task when an event occurs on a JMenuItem object, you need to add an event listener to that JMenuItem item object. Because a JMenuItem is a sub class of AbstractButton, it shares the listener interface and event class with AbstractButton:

java.awt.event.ActionListener - An AWT interface that allows you to implement your own button event handler methods:

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

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

/* MenuItemActionListenerTest.java
 * Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
import java.awt.event.*;
import javax.swing.*;
public class MenuItemActionListenerTest {
   JFrame myFrame = null;
   public static void main(String[] a) {
      (new MenuItemActionListenerTest()).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);
      myMenu = getOptionMenu();
      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");
      JMenuItem myItem = new MyMenuItem("Red");
      myMenu.add(myItem);
      myItem = new MyMenuItem("Green");
      myMenu.add(myItem);
      myItem = new MyMenuItem("Blue");
      myMenu.add(myItem);
      return myMenu;
   }
   private JMenu getOptionMenu() {
      JMenu myMenu = new JMenu("Option");
      JMenuItem myItem = new MyMenuItem("Sound");
      myMenu.add(myItem);
      myItem = new MyMenuItem("Auto save");
      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());
      }
   }
}

If you run this example, you will see the frame window shows up with the menu bar like this:
Menu Item Action Listener Test

If you click the "Help" menu item, click "Open" and "Close" in the "File" menu, then click "Red", "Green" and "Blue" in the "Color" menu, you will see some messages printed on the Java console window:

Item clicked: Help
Item clicked: Open
Item clicked: Close
Item clicked: Red
Item clicked: Green
Item clicked: Blue

Interesting notes about this tutorial example:

Sample programs listed in this section have been tested with JDK 1.6.0 to 1.8.0.

Last update: 2014.

Table of Contents

 About This Book

 Introduction of Java Swing Package

 Graphics Environment of the Local System

 JFrame - Main Frame Class

 JLabel - Swing Label Class

 JButton - Swing Button Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

Menu Bar, Menus, Menu Items and Listeners

 JMenuBar, JMenu, and JMenuItem Classes

 JMenuBarTest.java - Menu Bar Test Program

 JMenuTest.java - Menu Test Program

 JMenuItemTest.java - Menu Item Test Program

 JRadioButtonMenuItemTest.java - Radio Button Menu Item Test Program

 JCheckBoxMenuItemTest.java - Check Box Menu Item Test Program

 javax.swing.event.MenuListener - Menu Listener Interface

MenuItemActionListenerTest.java - 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

 References

 PDF Printing Version