Menu
Menu
s are the pull-down objects that appear on the MenuBar
of a Frame
or within other menus. They contain MenuItem
s or CheckboxMenuItem
s for the user to select. The Menu
class subclasses MenuItem
(so it can appear on a Menu
, too) and implements MenuContainer
. Tear-off menus are menus that can be dragged, placed elsewhere on the screen, and remain on the screen when the input focus moves to something else. Java supports tear-off menus if the underlying platform does. Motif (UNIX) supports tear-off menus; Microsoft Windows platforms do not.
Menu Methods
Constructors- public Menu ()
- The first constructor for
Menu
creates a menu that has no label and cannot be torn off. To set the label at a later time, usesetLabel()
. - public Menu (String label)
- This constructor for
Menu
creates aMenu
withlabel
displayed on it. TheMenu
cannot be torn off. - public Menu (String label, boolean tearOff)
- This constructor for
Menu
creates aMenu
withlabel
displayed on it. The handling oftearOff
is platform dependent.Figure 10.3 shows a tear-off menu for Windows/95 and Motif. Since Windows does not support tear-off menus, the Windows menu looks and acts like a regular menu.
Figure 10.3: Tear-off menu
Items
- public int getItemCount()
public int countItems () - The
getItemCount()
method returns the number of items within theMenu
. Only top-level items are counted: if an item is a submenu, this method doesn't include the items on it.countItems()
is the Java 1.0 name for this method. - public MenuItem getItem (int index)
- The
getItem()
method returns theMenuItem
at positionindex
. Ifindex
is invalid,getItem()
throws theArrayIndexOutOfBoundsException
run-time exception. - public synchronized MenuItem add (MenuItem item)
- The
add()
method putsitem
on the menu. The label assigned toitem
when it was created is displayed on the menu. Ifitem
is already in another menu, it is removed from that menu. Ifitem
is aMenu
, it creates a submenu. (Remember thatMenu
subclassesMenuItem
.) - public void add (String label)
- This version of
add()
creates aMenuItem
withlabel
as the text and adds that to the menu. Iflabel
is theString
"-", a separator bar is added to theMenu
. - public synchronized void insert(MenuItem item, int index)
- The
insert()
method putsitem
on the menu at positionindex
. The label assigned toitem
when it was created is displayed on the menu. Positions are zero based, and ifindex
< 0,insert()
throws theIllegalArgumentException
run-time exception. - public synchronized void insert(String label, int index)
- This version of
insert()
method creates aMenuItem
withlabel
as the text and adds that to the menu at positionindex
. Iflabel
is the String "--", a separator bar is added to theMenu
. Positions are zero based, and ifindex
< 0, this method throws theIllegalArgumentException
run-time exception. - public void addSeparator ()
- The
addSeparator()
method creates a separatorMenuItem
and adds that to the menu. Separator menu items are strictly cosmetic and do not generate events when selected. - public void insertSeparator(int index)
- The
insertSeparator()
method creates a separatorMenuItem
and adds that to the menu at positionindex
. Separator menu items are strictly cosmetic and do not generate events when selected. Positions are zero based. Ifindex
< 0,insertSeparator()
throws theIllegalArgumentException
run-time exception. - public synchronized void remove (int index)
- The
remove()
method removes theMenuItem
at positionindex
from theMenu
. Ifindex
is invalid,remove()
throws theArrayIndexOutOfBoundsException
run-time exception.index
is zero based, so it can range from 0 togetItemCount()-1
. - public synchronized void remove (MenuComponent component)
- This version of
remove()
removes the menu itemcomponent
from theMenu
. If component is not in theMenu
, nothing happens. - public synchronized void removeAll()
- The
removeAll()
removes allMenuItems
from theMenu
.
- public synchronized void addNotify ()
- The
addNotify()
method creates theMenu
peer with all theMenuItems
on it. - public synchronized void removeNotify ()
- The
removeNotify()
method destroys the peer of theMenuComponent
and removes it from the screen. The peers of the items on the menu are also destroyed.
- public boolean isTearOff ()
- The
isTearOff()
method returnstrue
if thisMenu
is a tear-off menu, andfalse
otherwise. Once a menu is created, there is no way to change the tear-off setting. This method can returntrue
even on platforms that do not support tear-off menus. - public String paramString ()
- The
paramString()
method ofMenu
should be protected like otherparamString()
methods. However, it is public so you have access to it. When you call thetoString()
method of aMenu
, the defaulttoString()
method ofMenuComponent
is called. This in turn callsparamString()
, which builds up the string to display. At theMenu
level, the setting forTearOff
(from constructor) and whether or not it is the help menu (fromMenuBar.setHelpMenu()
) for the menu bar are added. If the constructor for theMenu
wasnew Menu (`File`)
, the results oftoString()
would be:
java.awt.Menu [menu0,label=File,tearOff=false,isHelpMenu=false]
Menu Events
A Menu
does not generate any event when it is selected. An event is generated when a MenuItem
on the menu is selected, as long as it is not another Menu
. You can capture all the events that happen on a Menu
by overriding postEvent()
.