The currently selected menu or menu item in a JMenu
or JPopupMenu
is tracked by MenuSelectionManager
. To receive notification of changes to the currently selected menu or menu item, a change listener must be registered with the MenuSelectionManager
.
// Create a change listener and register with the menu selection manager MenuSelectionManager.defaultManager().addChangeListener( new ChangeListener() { public void stateChanged(ChangeEvent evt) { // Get the selected menu or menu item MenuSelectionManager msm = (MenuSelectionManager)evt.getSource(); MenuElement[] path = msm.getSelectedPath(); // To interpret path, see // e813 获得当前选择的菜单或菜单项 if (path.length == 0) { // All menus are hidden } } } );
Related Examples |