JMenu ActionListener

35,664
  • for JMenu use MenuListener

code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class ActionExample {

    public ActionExample() {

        JMenu menu = new JMenu("Menu");
        menu.setMnemonic(KeyEvent.VK_M);
        menu.addMenuListener(new SampleMenuListener());
        JMenu menu1 = new JMenu("Tool");
        menu1.setMnemonic(KeyEvent.VK_T);
        menu1.addMenuListener(new SampleMenuListener());
        JFrame f = new JFrame("ActionExample");
        JMenuBar mb = new JMenuBar();
        mb.add(menu);
        mb.add(menu1);
        f.setJMenuBar(mb);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ActionExample actionExample = new ActionExample();
            }
        });
    }
}

class SampleMenuListener implements MenuListener {

    @Override
    public void menuSelected(MenuEvent e) {
        System.out.println("menuSelected");
    }

    @Override
    public void menuDeselected(MenuEvent e) {
        System.out.println("menuDeselected");
    }

    @Override
    public void menuCanceled(MenuEvent e) {
        System.out.println("menuCanceled");
    }
}
Share:
35,664
clankfan1
Author by

clankfan1

Updated on July 26, 2022

Comments

  • clankfan1
    clankfan1 almost 2 years

    I was wondering if can you test to see if a JMenu (not JMenuItem) has been clicked. I tried adding an ActionListener to it but it doesn't seem to recognize it. I just need it to preform an action when the JMenu button is pressed so that I can change the JMenuItems for that menu befor it opens. All work arrounds to get this result are welcome too!

    Thanks

    • assylias
      assylias over 12 years
      Have you tried addMenuListener?
    • clankfan1
      clankfan1 over 12 years
      No I'm using addActionListener right now but to add that I run into some problems, shouldn't actionListener work aswell though?
    • Hovercraft Full Of Eels
      Hovercraft Full Of Eels over 12 years
      Why change the menu items on JMenu click? Why not change it before the click? What is your "use case" here?
    • clankfan1
      clankfan1 over 12 years
      mre, that only works for JMenuItems I need a listener for a JMenu
    • clankfan1
      clankfan1 over 12 years
      Hovercraft, I'm using it to determine whether or not certain JMenuItems are enabled and I thought it would be an easy way to test it without using threads...
    • Andrew Thompson
      Andrew Thompson over 12 years
      "determine whether or not certain JMenuItems are enabled" setEnabled(boolean) is not enough?
    • Hovercraft Full Of Eels
      Hovercraft Full Of Eels over 12 years
      I'm still not quite sure what your goal is. You can trap Menu clicks with a ChangeListener, however the rationale has a funny smell to me.
  • Hummeling Engineering BV
    Hummeling Engineering BV about 2 years
    In this case, where we're only overriding a single method, use MouseAdapter for cleaner code.
  • Hummeling Engineering BV
    Hummeling Engineering BV about 2 years
    We can add an ActionListener to JMenu, but it just doesn't have any effect. JMenu opens its child JMenus and JMenuItems when the mouse hovers over it. I think the question is about the extra functionality to perform an action when clicking on the JMenu.