java.lang.Object java.awt.MenuComponent java.awt.MenuItem
public class MenuItem
All items in a menu must belong to the class MenuItem
, or one of its subclasses.
The default MenuItem
object embodies a simple labeled menu item.
This picture of a menu bar shows five menu items:
The first two items are simple menu items, labeled "Basic"
and "Simple"
. Following these two items is a separator, which is itself a menu item, created with the label "-"
. Next is an instance of CheckboxMenuItem
labeled "Check"
. The final menu item is a submenu labeled "More Examples"
, and this submenu is an instance of Menu
.
When a menu item is selected, AWT sends an action event to the menu item. Since the event is an instance of ActionEvent
, the processEvent
method examines the event and passes it along to processActionEvent
. The latter method redirects the event to any ActionListener
objects that have registered an interest in action events generated by this menu item.
Note that the subclass Menu
overrides this behavior and does not send any event to the frame until one of its subitems is selected.
MenuItem()
MenuItem(String label)
MenuItem(String label, MenuShortcut s)
void
addActionListener(ActionListener l)
void
addNotify()
void
deleteShortcut()
MenuShortcut
object associated with this menu item. void
disable()
setEnabled(boolean)
. protected void
disableEvents(long eventsToDisable)
void
enable()
setEnabled(boolean)
. void
enable(boolean b)
setEnabled(boolean)
. protected void
enableEvents(long eventsToEnable)
AccessibleContext
getAccessibleContext()
String
getActionCommand()
ActionListener[]
getActionListeners()
String
getLabel()
<T extends EventListener>
T[]
getListeners(Class<T> listenerType)
FooListener
s upon this MenuItem
. MenuShortcut
getShortcut()
MenuShortcut
object associated with this menu item, boolean
isEnabled()
String
paramString()
MenuItem
. protected void
processActionEvent(ActionEvent e)
ActionListener
objects. protected void
processEvent(AWTEvent e)
void
removeActionListener(ActionListener l)
void
setActionCommand(String command)
void
setEnabled(boolean b)
void
setLabel(String label)
void
setShortcut(MenuShortcut s)
MenuShortcut
object associated with this menu item. Methods inherited from class java.awt.MenuComponent dispatchEvent, getFont, getName, getParent, getPeer, getTreeLock, postEvent, removeNotify, setFont, setName, toString
MenuItem
public MenuItem() throws HeadlessException
HeadlessException
- if GraphicsEnvironment.isHeadless() returns true.
GraphicsEnvironment.isHeadless()
public MenuItem(String label) throws HeadlessException
label
- the label for this menu item.
HeadlessException
- if GraphicsEnvironment.isHeadless() returns true.
GraphicsEnvironment.isHeadless()
public MenuItem(String label, MenuShortcut s) throws HeadlessException
label
- the label for this menu item.
s
- the instance of MenuShortcut
associated with this menu item.
HeadlessException
- if GraphicsEnvironment.isHeadless() returns true.
GraphicsEnvironment.isHeadless()
public void addNotify()
public String getLabel()
null
if this menu item has no label.
setLabel(java.lang.String)
public void setLabel(String label)
label
- the new label, or null
for no label.
getLabel()
public boolean isEnabled()
setEnabled(boolean)
public void setEnabled(boolean b)
b
- if true
, enables this menu item; if false
, disables it.
isEnabled()
@Deprecated public void enable()
setEnabled(boolean)
.
@Deprecated public void enable(boolean b)
setEnabled(boolean)
.
@Deprecated public void disable()
setEnabled(boolean)
.
public MenuShortcut getShortcut()
MenuShortcut
object associated with this menu item,
null
if none has been specified.
setShortcut(java.awt.MenuShortcut)
public void setShortcut(MenuShortcut s)
MenuShortcut
object associated with this menu item. If a menu shortcut is already associated with this menu item, it is replaced.
s
- the menu shortcut to associate with this menu item.
getShortcut()
public void deleteShortcut()
MenuShortcut
object associated with this menu item.
protected final void enableEvents(long eventsToEnable)
Since event types are automatically enabled when a listener for that type is added to the menu item, this method only needs to be invoked by subclasses of MenuItem
which desire to have the specified event types delivered to processEvent
regardless of whether a listener is registered.
eventsToEnable
- the event mask defining the event types
processEvent(java.awt.AWTEvent)
, disableEvents(long)
, Component.enableEvents(long)
protected final void disableEvents(long eventsToDisable)
eventsToDisable
- the event mask defining the event types
processEvent(java.awt.AWTEvent)
, enableEvents(long)
, Component.disableEvents(long)
public void setActionCommand(String command)
By default, the action command is set to the label of the menu item.
command
- the action command to be set for this menu item.
getActionCommand()
public String getActionCommand()
setActionCommand(java.lang.String)
public void addActionListener(ActionListener l)
l
- the action listener.
removeActionListener(java.awt.event.ActionListener)
, getActionListeners()
, ActionEvent
, ActionListener
public void removeActionListener(ActionListener l)
l
- the action listener.
addActionListener(java.awt.event.ActionListener)
, getActionListeners()
, ActionEvent
, ActionListener
public ActionListener[] getActionListeners()
ActionListener
s or an empty array if no action listeners are currently registered
addActionListener(java.awt.event.ActionListener)
, removeActionListener(java.awt.event.ActionListener)
, ActionEvent
, ActionListener
public <T extends EventListener> T[] getListeners(Class<T> listenerType)
FooListener
s upon this MenuItem
. FooListener
s are registered using the addFooListener
method.
You can specify the listenerType
argument with a class literal, such as FooListener.class
. For example, you can query a MenuItem
m
for its action listeners with the following code:
ActionListener[] als = (ActionListener[])(m.getListeners(ActionListener.class));If no such listeners exist, this method returns an empty array.
listenerType
- the type of listeners requested; this parameter should specify an interface that descends from java.util.EventListener
FooListener
s on this menu item, or an empty array if no such listeners have been added
ClassCastException
- if listenerType
doesn't specify a class or interface that implements java.util.EventListener
getActionListeners()
protected void processEvent(AWTEvent e)
ActionEvent
, it invokes processActionEvent
, another method defined by MenuItem
.
Currently, menu items only support action events.
Note that if the event parameter is null
the behavior is unspecified and may result in an exception.
processEvent
in class MenuComponent
e
- the event
processActionEvent(java.awt.event.ActionEvent)
protected void processActionEvent(ActionEvent e)
ActionListener
objects. This method is not called unless action events are enabled for this component. Action events are enabled when one of the following occurs:
ActionListener
object is registered via addActionListener
.enableEvents
.Note that if the event parameter is null
the behavior is unspecified and may result in an exception.
e
- the action event
ActionEvent
, ActionListener
, enableEvents(long)
public String paramString()
MenuItem
. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null
.
paramString
in class MenuComponent
public AccessibleContext getAccessibleContext()
getAccessibleContext
in interface Accessible
getAccessibleContext
in class MenuComponent
Copyright © 2004, 2010 Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4