Home » Desktop Java » awt » event » MenuListener example

About Ilias Tsagklis

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

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() {

  super();

  MenuListener listener = new MenuListener() {

@Override

public void menuCanceled(MenuEvent event) {

    printEcentInfo("Canceled", event);

}

@Override

public void menuDeselected(MenuEvent event) {

    printEcentInfo("Deselected", event);

}

@Override

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.addMenuListener(listener);

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

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

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

  JMenu hmenu = new JMenu("Help");

  hmenu.addMenuListener(listener);

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

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

  hmenu.addSeparator();

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

  JMenu sub = new JMenu("Categories");

  sub.addMenuListener(listener);

  JRadioButtonMenuItem radioMenu;

  ButtonGroup buttonGroup = new ButtonGroup();

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

  buttonGroup.add(radioMenu);

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

  buttonGroup.add(radioMenu);

  hmenu.add(sub);

  JMenuBar menuBar = new JMenuBar();

  menuBar.add(fMenu);

  menuBar.add(hmenu);

  setJMenuBar(menuBar);
    }

    public static void main(String args[]) {

  JFrame jFrame = new MenuListenerExample();

  jFrame.setSize(600, 400);

  jFrame.setVisible(true);
    }
}

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

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

 

Leave a Reply

avatar
  Subscribe  
Notify of