MenuItem
A MenuItem
is the basic item that goes on a Menu
. Menus themselves are menu items, allowing submenus to be nested inside of menus. MenuItem
is a subclass of MenuComponent
.
MenuItem Methods
Constructors- public MenuItem ()
- The first
MenuItem
constructor creates aMenuItem
with an empty label and no keyboard shortcut. To set the label at later time, usesetLabel()
. - public MenuItem (String label)
- This
MenuItem
constructor creates aMenuItem
with a label oflabel
and no keyboard shortcut. A label of "-" represents a separator. - public MenuItem (String label, MenuShortcut shortcut)
- The final
MenuItem
constructor creates aMenuItem
with a label oflabel
and aMenuShortcut
ofshortcut
. Pressing the shortcut key is the same as selecting the menu item.
Each MenuItem
has a label. This is the text that is displayed on the menu.
NOTE:
Prior to Java 1.1, there was no portable way to associate a hot key with a MenuItem
. However, in Java 1.0, if you precede a character with an & on a Windows platform, it will appear underlined, and that key will act as the menu's mnemonic key (a different type of shortcut from MenuShortcut
). Unfortunately, on a Motif platform, the user will see the &. Because the & is part of the label, even if it is not displayed, you must include it explicitly whenever you compare the label to a string.
- public String getLabel ()
- The
getLabel()
method retrieves the label associated with theMenuItem
. - public void setLabel (String label)
- The
setLabel()
method changes the label of theMenuItem
tolabel
.
- public MenuShortcut getMenuShortcut ()
- The
getMenuShortcut()
method retrieves the shortcut associated with thisMenuItem
. - public void setShortcut (MenuShortcut shortcut)
- The
setShortcut()
method allows you to change the shortcut associated with aMenuItem
toshortcut
after theMenuItem
has been created. - public void deleteMenuShortcut ()
- The
deleteMenuShortcut()
method removes any associatedMenuShortcut
from theMenuItem
. If there was no shortcut, nothing happens.
- public boolean isEnabled ()
- The
isEnabled()
method checks to see if theMenuItem
is currently enabled. An enabledMenuItem
can be selected by the user. A disabledMenuItem
, by convention, appears grayed out on theMenu
. Initially, eachMenuItem
is enabled. - public synchronized void setEnabled(boolean b)
public void enable (boolean condition) - The
setEnabled()
method either enables or disables theMenuItem
based on the value ofcondition
. Ifcondition
istrue
, theMenuItem
is enabled. Ifcondition
isfalse
, it is disabled. When enabled, the user can select it, generatingACTION_EVENT
or notifying theActionListener
. When disabled, the peer does not generate anACTION_EVENT
if the user tries to select theMenuItem
. A disabledMenuItem
is usually grayed out to signify its state. The way that disabling is signified is platform specific.enable()
is the Java 1.0 name for this method. - public synchronized void enable ()
- The
enable()
method enables theMenuItem
. In Java 1.1, it is better to usesetEnabled()
. - public synchronized void disable ()
- The
disable()
method disables the component so that the user cannot select it. In Java 1.1, it is better to usesetEnabled()
.
- public synchronized void addNotify ()
- The
addNotify()
method creates theMenuItem
peer. - public String paramString ()
- The
paramString()
method ofMenuItem
should be protected like otherparamString()
methods. However, it is public so you have access to it. When you call thetoString()
method of aMenuItem
, the defaulttoString()
method ofMenuComponent
is called. This in turn callsparamString()
which builds up the string to display. At theMenuItem
level, the current label of the object and the shortcut (if present) is appended to the output. If the constructor for theMenuItem
wasnew MenuItem(`File`)
, the results oftoString()
would be:
java.awt.MenuItem[label=File]
MenuItem Events
Event handlingWith 1.0 event handing, a MenuItem
generates an ACTION_EVENT
when it is selected. The argument to action()
will be the label of the MenuItem
. But the target of the ACTION_EVENT
is the Frame
containing the menu. You cannot subclass MenuItem
and catch the Event
within it with action()
, but you can with postEvent()
. No other events are generated for MenuItem
instances.
- public boolean action (Event e, Object o)--overridden by user, called by system
- The
action()
method for aMenuItem
signifies that the user selected it.e
is theEvent
instance for the specific event, whileo
is the label of theMenuItem
.
With the 1.1 event model, you register listeners, and they are told when the event happens.
- public String getActionCommand()
- The
getActionCommand()
method retrieves the command associated with thisMenuItem
. By default, it is the label. However, the default can be changed by using thesetActionCommand()
method (described next). The command acts like the second parameter to theaction()
method in the 1.0 event model. - public void setActionCommand(String command)
- The
setActionCommand()
method changes the command associated with aMenuItem
. When anActionEvent
happens, thecommand
is part of the event. By default, this would be the label of theMenuItem
. However, you can change the action command by calling this method. Using action commands is a good idea, particularly if you expect your code to run in a multilingual environment. - public void addActionListener(ItemListener listener)
- The
addActionListener()
method registerslistener
as an object interested in being notified when anActionEvent
passes through theEventQueue
with thisMenuItem
as its target. Thelistener.actionPerformed()
method is called whenever these events occur. Multiple listeners can be registered. - public void removeActionListener(ItemListener listener)
- The
removeActionListener()
method removeslistener
as an interested listener. Iflistener
is not registered, nothing happens. - protected final void enableEvents(long eventsToEnable)
- Using the
enableEvents()
method is usually not necessary. When you register an action listener, theMenuItem
listens for action events. However, if you wish to listen for events when listeners are not registered, you must enable the events explicitly by calling this method. The settings for theeventsToEnable
parameter are found in theAWTEvent
class; you can use any of theEVENT_MASK
constants likeCOMPONENT_EVENT_MASK
,MOUSE_EVENT_MASK
, andWINDOW_EVENT_MASK
ORed together for the events you care about. For instance, to listen for action events, call:
enableEvents (AWTEvent.ACTION_EVENT_MASK);
- protected final void disableEvents(long eventsToDisable)
- Using the
disableEvents()
method is usually not necessary. When you remove an action listener, theMenuItem
stops listening for action events if there are no more listeners. However, if you need to, you can disable events explicitly by callingdisableEvents()
. The settings for theeventsToDisable
parameter are found in theAWTEvent
class; you can use any of theEVENT_MASK
constants such asFOCUS_EVENT_MASK
,MOUSE_MOTION_EVENT_MASK
, andACTION_EVENT_MASK
ORed together for the events you no longer care about. - protected void processEvent(AWTEvent e)
- The
processEvent()
method receives allAWTEvent
s with thisMenuItem
as its target.processEvent()
then passes them along to any listeners for processing. When you subclassMenuItem
, overridingprocessEvent()
allows you to process all events yourself, before sending them to any listeners. In a way, overridingprocessEvent()
is like overridingpostEvent()
using the 1.0 event model.If you override
processEvent()
, remember to callsuper.processEvent(e)
last to ensure that regular event processing can occur. If you want to process your own events, it's a good idea to callenableEvents()
to ensure that events are delivered even in the absence of registered listeners. - protected void processActionEvent(ItemEvent e)
- The
processActionEvent()
method receives allActionEvent
s with thisMenuItem
as its target.processActionEvent()
then passes them along to any listeners for processing. When you subclassMenuItem
, overridingprocessActionEvent()
allows you to process all action events yourself, before sending them to any listeners. In a way, overridingprocessActionEvent()
is like overridingaction()
using the 1.0 event model.If you override
processActionEvent()
, remember to call the methodsuper.processActionEvent(e)
last to ensure that regular event processing can occur. If you want to process your own events, it's a good idea to callenableEvents()
to ensure that events are delivered even in the absence of registered listeners.