Frage

The default behaviour when you click a JMenu (or when you click an adjacent JMenu and then drag the mouse over it) is to unfold and show its submenus and menu items. Can I associate additional behaviour (like generating an event) when this (when the menu is unfolded) happens?

War es hilfreich?

Lösung

There are event listener classes specifically for JMenus:

javax.swing.event.MenuListener

and

javax.swing.event.MenuDragMouseListener

The methods you need to call to associate these to a component are

JMenuComponentName.addMenuListener(MenuListener m); and JMenuComponentName.addMenuMouseDragListener(MenuDragMouseListener m);, respectively.

Andere Tipps

You can make use of the MenuListener which should provide you information about the state of the menu.

This simple example creates a new menu item each time the menu is opened...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

public class TestMenuBar {

    public static void main(String[] args) {
        new TestMenuBar();
    }

    public TestMenuBar() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JMenuBar mb = new JMenuBar();
                JMenu main = new JMenu("Test");
                mb.add(main);

                main.addMenuListener(new MenuListener() {
                    @Override
                    public void menuSelected(MenuEvent e) {
                        System.out.println("Selected");
                        JMenu menu = (JMenu) e.getSource();

                        menu.add(new JMenuItem("I'm dynamiclly created"));
                    }

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

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

                JFrame frame = new JFrame("Testing");
                frame.setJMenuBar(mb);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }        
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top