MenuItemActionListenerTest.java - AWT Menu Item Action Listener

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

Like any other user interface components, MenuItem objects also fire events when users interact with them. If you want to perform a task when an event occurs on a MenuItem object, you need to add an event listener to that MenuItem item object. Because a MenuItem 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 event handler methods:

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

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

/* MenuItemActionListenerTest.java
 * Copyright (c) 1997-2024 HerongYang.com. All Rights Reserved.
 */
import java.awt.Frame;
import java.awt.MenuBar;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MenuItemActionListenerTest {
   Frame myFrame = null;
   public static void main(String[] a) {
      (new MenuItemActionListenerTest()).test();
   }
   private void test() {
      myFrame = new Frame("Menu Listener Test");
      myFrame.setBounds(50,50,250,150);

      MenuBar myMenuBar = new MenuBar();
      Menu myMenu = getFileMenu();
      myMenuBar.add(myMenu);
      myMenu = getColorMenu();
      myMenuBar.add(myMenu);
      myMenu = getOptionMenu();
      myMenuBar.add(myMenu);

      myFrame.setMenuBar(myMenuBar);
      myFrame.setVisible(true);
   }
   private Menu getFileMenu() {
      Menu myMenu = new Menu("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 Menu getColorMenu() {
      Menu myMenu = new Menu("Color");
      MenuItem myItem = new MyMenuItem("Red");
      myMenu.add(myItem);
      myItem = new MyMenuItem("Green");
      myMenu.add(myItem);
      myItem = new MyMenuItem("Blue");
      myMenu.add(myItem);
      return myMenu;
   }
   private Menu getOptionMenu() {
      Menu myMenu = new Menu("Option");
      MenuItem myItem = new MyMenuItem("Sound");
      myMenu.add(myItem);
      myItem = new MyMenuItem("Auto save");
      myMenu.add(myItem);
      return myMenu;
   }
   private class MyMenuItem extends MenuItem 
      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:

AWT enu Item Action Listener Test
AWT 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:

Note that there seems to be a problem with the f.setMenuBar(mb) call on macOS computers. It fails to display the menu bar.

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

 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)

 What Is AWT (Abstract Windows Toolkit)

 HelloAWT.java - My First AWT Program

 Closing AWT Frame and Terminating Application

 Drawing Graphics - Using paint() on Frame or Component

 Creating Labels with java.awt.Label Class

 Creating Buttons with java.awt.Button Class

 AWT Button Action Handler at the Component Level

 AWT Button Action Handler at the Frame Level

 AWT Button Mouse Click Handler at the Frame Level

 AWT TextField and ActionListener

 MenuBarTest.java - AWT Menu Bar Test Program

 MenuTest.java - AWT Menu Test Program

 MenuItemTest.java - AWT Menu Item Test Program

MenuItemActionListenerTest.java - AWT Menu Item Action Listener

 Integration with Desktop System

 Archived Tutorials

 References

 Full Version in PDF/EPUB