Java Reference
In-Depth Information
public class MenuKeyEvent extends KeyEvent {
public MenuKeyEvent(Component source, int id, long when, int modifiers,
int keyCode, char keyChar, MenuElement path[], MenuSelectionManager mgr);
public MenuSelectionManager getMenuSelectionManager();
public MenuElement[] getPath();
}
It's the job of the
MenuSelectionManager
to determine the current selection path. The
selection path is the set of menu elements from the top-level
JMenu
on the
JMenuBar
to the
selected components. For the most part, the manager works behind the scenes, and you never
need to worry about it.
Listening to JMenuItem Events with a MenuDragMouseListener
Like
MenuKeyEvent
, the
MenuDragMouseEvent
is a special kind of event used internally by the user
interface classes for
JMenu
and
JMenuItem
. As its name implies, the
MenuDragMouseEvent
is a
special kind of
MouseEvent
. By monitoring when a mouse is moved within an open menu, the
user interface classes use the listener to maintain the selection path, thus determining the
currently selected menu item. Its definition follows:
public interface MenuDragMouseListener extends EventListener {
public void menuDragMouseDragged(MenuDragMouseEvent e);
public void menuDragMouseEntered(MenuDragMouseEvent e);
public void menuDragMouseExited(MenuDragMouseEvent e);
public void menuDragMouseReleased(MenuDragMouseEvent e);
}
As with the
MenuKeyListener
, normally you don't listen for this event yourself. If you're
interested in when a menu or submenu is about to be displayed, the better listener to register
is the
MenuListener
, which can be registered with the
JMenu
, but not with an individual
JMenuItem
.
You'll look at this in the next section, which describes
JMenu
.
The definition of the
MenuDragMouseEvent
class, the argument to each of the
MenuDragMouseListener
methods, is as follows:
public class MenuDragMouseEvent extends MouseEvent {
public MenuDragMouseEvent(Component source, int id, long when, int modifiers,
int x, int y, int clickCount, boolean popupTrigger, MenuElement path[],
MenuSelectionManager mgr);
public MenuSelectionManager getMenuSelectionManager();
public MenuElement[] getPath();
}
Customizing a JMenuItem Look and Feel
As with the
JMenuBar
, the predefined look and feel types each provide a different
JMenuItem
appearance and set of default
UIResource
values. Figure 6-3 showed the appearance of the
JMenuItem
component for the preinstalled set: Motif, Windows, and Ocean.
The available set of
UIResource
-related properties for a
JMenuItem
are shown in Table 6-4.
The
JMenuItem
component offers 20 different properties.