event

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.

Ilias Tsagklis

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

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

0 Comments
Inline Feedbacks
View all comments
Back to top button