MenuListener example

With this example we shall show you how the MenuListener interface works in Java. Menu components are very usual in rich, Graphical applications and they contribute to a better user experience.

So, a MenuListener component can be very useful when your application has many menu items and it’s important to monitor the user events and the user interactions with each one of them.

In order to use a MenuListener, one should take the following steps:

  • Create a new MenuListener.
  • Override the functions that correspond to the menu events you want to monitor e.g menuCanceled, menuDeselected, menuSelected to customize the handling of the respective events.
  • Create a JMenu component
  • Use the addMenuListener method of the JMenu component to add to it the MenuListener you’ve created.

Let’s take a look at the code snippets that follow:

package com.javacodegeeks.snippets.desktop;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

public class MenuListenerExample extends JFrame {

    public MenuListenerExample() {


  MenuListener listener = new MenuListener() {


public void menuCanceled(MenuEvent event) {

    printEcentInfo("Canceled", event);



public void menuDeselected(MenuEvent event) {

    printEcentInfo("Deselected", event);



public void menuSelected(MenuEvent event) {

    printEcentInfo("Selected", event);


private void printEcentInfo(String s, MenuEvent event) {

    JMenu menu = (JMenu) event.getSource();

    System.out.println(s + ": " + menu.getText());



  JMenu fMenu = new JMenu("File");


  fMenu.add(new JMenuItem("Open"));

  fMenu.add(new JMenuItem("Close"));

  fMenu.add(new JMenuItem("Exit"));

  JMenu hmenu = new JMenu("Help");


  hmenu.add(new JMenuItem("About MenuTest"));

  hmenu.add(new JMenuItem("Class Hierarchy"));


  hmenu.add(new JCheckBoxMenuItem("More Help"));

  JMenu sub = new JMenu("Categories");


  JRadioButtonMenuItem radioMenu;

  ButtonGroup buttonGroup = new ButtonGroup();

  sub.add(radioMenu = new JRadioButtonMenuItem("Some Help", true));


  sub.add(radioMenu = new JRadioButtonMenuItem("Help"));



  JMenuBar menuBar = new JMenuBar();




    public static void main(String args[]) {

  JFrame jFrame = new MenuListenerExample();

  jFrame.setSize(600, 400);


This was an example on how to use MenuListener in Java.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button