java action listener on menu, and not on menu item

2020-02-06 08:56发布

I need enlightenment.

how to add action actionListener event bind to the menu, and not bind to the menu ITEM here is the demo code, that works(on menuITEM)..

menuFileItem.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("It works");
                }
            }
        );

but when i try the same , but just on the MENU itself it doesn't work!

menuFile.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("Plz work... :( ");
                }
            }
        );

is it possible to add listener to menu? i taught listener could be added to everything.

4条回答
萌系小妹纸
2楼-- · 2020-02-06 09:29

based on the previous answers, I added an additional key listener to the solution.

Here is a convenience function:

public static void bind(JMenu menu, ActionListener listener) {

        menu.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                listener.actionPerformed(new ActionEvent(e.getSource(), ActionEvent.ACTION_PERFORMED, "Delegate"));
            }
        });

        menu.addMenuKeyListener(new MenuKeyListener() {

            @Override
            public void menuKeyPressed(MenuKeyEvent e) {

                if(e.getKeyCode() != KeyEvent.VK_ENTER){
                    return;
                }

                // Find out if the menu is the currently selected menu element
                MenuElement[] selectionPath = e.getMenuSelectionManager().getSelectedPath();

                // Because the JMenu will open a popup menu, the menu is the penultimate element in the path
                if (selectionPath.length < 2) {
                    return;
                }

                MenuElement selectedMenu = selectionPath[selectionPath.length - 2];

                if (selectedMenu == menu) {
                    listener.actionPerformed(new ActionEvent(e.getSource(), ActionEvent.ACTION_PERFORMED, "Delegate"));
                }
            }

            @Override
            public void menuKeyTyped(MenuKeyEvent e) {}

            @Override
            public void menuKeyReleased(MenuKeyEvent e) {}
        });
    } 
查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-02-06 09:43

I use this addMouseListener(), because when you click over a JMenu, it marks it blue and executes the event just after.

I think it's that effect that you want.

Here it works using this code: I declare:

JMenu jmCadastrar = new JMenu("Cadastrar");

jmCadastrar.addMouseListener(new MouseListener() {

        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub
            System.exit(0);
        }
    });

}
查看更多
贪生不怕死
4楼-- · 2020-02-06 09:50

You can add an ActionListener to a JMenu as this method is inherited from AbstractButton. (JMenu Documentation)

JMenu menu = new JMenu("MyMenu");
menu.addActionListener(new ActionListener(){...});

But, it is not intended to be used this way: JMenu ignores ActionEvent. You should use MenuEvent and MenuListener instead.

JMenu menu = new JMenu("MyMenu");
menu.addMenuListener(new 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");

        }
    });
查看更多
贪生不怕死
5楼-- · 2020-02-06 09:52

If you instantiated JMenu class to bulid a menu object, try the method addMenuListener().

查看更多
登录 后发表回答