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:

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

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

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

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

      myMenu = getColorMenu();

      myMenu = getOptionMenu();

      JMenuItem myItem = new JMenuItem("Help");

   private JMenu getFileMenu() {
      JMenu myMenu = new JMenu("File");
      JMenuItem myItem = new JMenuItem("Open");
      myItem = new JMenuItem("Close");
      myItem = new JMenuItem("Exit");
      return myMenu;
   private JMenu getColorMenu() {
      JMenu myMenu = new JMenu("Color");
      JMenuItem myItem = new JMenuItem("Red");
      myItem = new JMenuItem("Green");
      myItem = new JMenuItem("Blue");
      return myMenu;
   private JMenu getOptionMenu() {
      JMenu myMenu = new JMenu("Option");
      JMenuItem myItem = new JMenuItem("Sound");
      myItem = new JMenuItem("Auto save");
      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 canceled: "+myMenu.getText());

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

Menu Listener Test
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:

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