Java Swing Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.01

javax.swing.event.MenuListener - Menu Listener Interface

This section provides a tutorial example on how to use javax.swing.event.MenuListener, menu listener interface, to catch events fired by JMenu objects.

Like any other user interface components, JMenu objects fire events when users interact with them. If you want to perform a task when an event occurs on a JMenu object, you need to add an event listener to that JMenu object. To do this, you need know these Swing classes, interfaces and methods:

javax.swing.event.MenuListener - A Swing interface that allows you to implement your own menu event handler methods:

  • menuSelected(MenuEvent) - Event handler method called when the associated menu is selected. You need to implement this method to perform your own task.
  • menuDeselected(MenuEvent) - Event handler method called when the associated menu is deselected. You need to implement this method to perform your own task.
  • menuCanceled(MenuEvent) - Event handler method called when the associated menu is canceled. You need to implement this method to perform your own task.

javax.swing.event.MenuEvent - A Swing class that represents an event occurred on a menu. The most important method in this class is:

  • getSource() - Method returns the JMenu object where the event occurred.

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

/**
 * MenuListenerTest.java
 * Copyright (c) 2009 by Dr. Herong Yang, http://www.herongyang.com/
 */
import javax.swing.*;
import javax.swing.event.*;
public class MenuListenerTest implements MenuListener {
   JFrame myFrame = null;
   public static void main(String[] a) {
      (new MenuListenerTest()).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();
      myMenu.addMenuListener(this);
      myMenuBar.add(myMenu);

      myMenu = getColorMenu();
      myMenu.addMenuListener(this);
      myMenuBar.add(myMenu);

      myMenu = getOptionMenu();
      myMenu.addMenuListener(this);
      myMenuBar.add(myMenu);

      JMenuItem myItem = new JMenuItem("Help");
      myMenuBar.add(myItem);

      myFrame.setJMenuBar(myMenuBar);
      myFrame.setVisible(true);
   }
   private JMenu getFileMenu() {
      JMenu myMenu = new JMenu("File");
      JMenuItem myItem = new JMenuItem("Open");
      myMenu.add(myItem);
      myItem = new JMenuItem("Close");
      myMenu.add(myItem);
      myMenu.addSeparator();
      myItem = new JMenuItem("Exit");
      myMenu.add(myItem);
      return myMenu;
   }
   private JMenu getColorMenu() {
      JMenu myMenu = new JMenu("Color");
      JMenuItem myItem = new JMenuItem("Red");
      myMenu.add(myItem);
      myItem = new JMenuItem("Green");
      myMenu.add(myItem);
      myItem = new JMenuItem("Blue");
      myMenu.add(myItem);
      return myMenu;
   }
   private JMenu getOptionMenu() {
      JMenu myMenu = new JMenu("Option");
      JMenuItem myItem = new JMenuItem("Sound");
      myMenu.add(myItem);
      myItem = new JMenuItem("Auto save");
      myMenu.add(myItem);
      return myMenu;
   }
   public void menuSelected(MenuEvent e) {
      JMenu myMenu = (JMenu) e.getSource();
      System.out.println("Menu Selected: "+myMenu.getText());
   }
   public void menuDeselected(MenuEvent e) {
      JMenu myMenu = (JMenu) e.getSource();
      System.out.println("Menu deselected: "+myMenu.getText());
   }
   public void menuCanceled(MenuEvent e) {
      JMenu myMenu = (JMenu) e.getSource();
      System.out.println("Menu anceled: "+myMenu.getText());
   }
}

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

If you click the "File" menu, move to the "Color" menu, then move to the "Option" menu, you will see some messages printed on the Java console window:

Menu Selected: File
Menu deselected: File
Menu Selected: Color
Menu deselected: Color
Menu Selected: Option

Interesting notes about this tutorial example:

  • "public class MenuListenerTest implements MenuListener" declaration is used to make my MenuListenerTest object becoming a MenuListener object.
  • "myMenu.addMenuListener(this);" statement is used to add a MenuListener object to a JMenu object.
  • "JMenu myMenu = (JMenu) e.getSource();" statement is used to get the JMenu object where the event was fired.
  • "myMenu.getText()" expression is used to get the menu button text.

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

Last update: 2009.

Table of Contents

 About This Java Swing Tutorial 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

 References

 Printable Copy - PDF Version

Dr. Herong Yang, updated in 2009
javax.swing.event.MenuListener - Menu Listener Interface