represents the anchored menus attached to a
JMenuBar or another
JMenu. Menus directly attached to a menu bar
are called top-level menus. Submenus, on the other hand, are not attached to a menu
bar but to a menu item that serves as its title. This menu item title is
typically marked by a right arrow, indicating that its menu appears
alongside the menu item if the user selects it. See Figure 14-11.
Figure 14-11. Top-level menu and submenu
JMenu is a curious class. It
MenuUI delegate, but it
ButtonModel for its data
model. To see why this is the case, it helps to visualize a menu as two components: a menu item and a pop-up menu. The menu item serves as the title. When it is
pressed, it signals the pop-up menu to show itself either below or
directly to the right of the menu item.
JMenu actually extends the
JMenuItem class, which makes it possible to
implement the title portion of the menu. This, in effect, makes it a
specialized button. On some platforms you can use the
mnemonic property of the
JMenuItem superclass to define a shortcut for
the menu’s title and, consequently, the menu. In addition, you can use
enabled property of
to disable the menu if desired.
As with pop-up menus, you can add or insert
Action objects in the pop-up portion of the
menu by calling the
add( ) and
insert( ) methods. ...