![]() |
The Java Developers Almanac 1.4 |
|
e815. Listening for Changes to the Currently Selected Menu or Menu ItemThe currently selected menu or menu item in aJMenu 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 Getting the Currently Selected Menu or Menu Item
if (path.length == 0) {
// All menus are hidden
}
}
}
);
e809. Separating Menu Items in a Menu e810. Creating a Popup Menu e811. Creating a Popup Menu with Nested Menus e812. Forcing a Popup Menu to Be a Heavyweight Component e813. Getting the Currently Selected Menu or Menu Item e814. Creating a Menu Item That Listens for Changes to Its Selection Status © 2002 Addison-Wesley. |