- java.lang.Object
-
- java.awt.MenuComponent
-
- java.awt.MenuItem
-
- All Implemented Interfaces:
- Serializable, Accessible
- Direct Known Subclasses:
- CheckboxMenuItem, Menu
public class MenuItem extends MenuComponent implements Accessible
All items in a menu must belong to the classMenuItem
, 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 ofCheckboxMenuItem
labeled"Check"
. The final menu item is a submenu labeled"More Examples"
, and this submenu is an instance ofMenu
.When a menu item is selected, AWT sends an action event to the menu item. Since the event is an instance of
ActionEvent
, theprocessEvent
method examines the event and passes it along toprocessActionEvent
. The latter method redirects the event to anyActionListener
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.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description protected class
MenuItem.AccessibleAWTMenuItem
Inner class of MenuItem used to provide default support for accessibility.-
Nested classes/interfaces inherited from class java.awt.MenuComponent
MenuComponent.AccessibleAWTMenuComponent
-
-
Constructor Summary
Constructors Constructor and Description MenuItem()
Constructs a new MenuItem with an empty label and no keyboard shortcut.MenuItem(String label)
Constructs a new MenuItem with the specified label and no keyboard shortcut.MenuItem(String label, MenuShortcut s)
Create a menu item with an associated keyboard shortcut.
-
Method Summary
Methods Modifier and Type Method and Description void
addActionListener(ActionListener l)
Adds the specified action listener to receive action events from this menu item.void
addNotify()
Creates the menu item's peer.void
deleteShortcut()
Delete anyMenuShortcut
object associated with this menu item.void
disable()
Deprecated.As of JDK version 1.1, replaced bysetEnabled(boolean)
.protected void
disableEvents(long eventsToDisable)
Disables event delivery to this menu item for events defined by the specified event mask parameter.void
enable()
Deprecated.As of JDK version 1.1, replaced bysetEnabled(boolean)
.void
enable(boolean b)
Deprecated.As of JDK version 1.1, replaced bysetEnabled(boolean)
.protected void
enableEvents(long eventsToEnable)
Enables event delivery to this menu item for events to be defined by the specified event mask parameterAccessibleContext
getAccessibleContext()
Gets the AccessibleContext associated with this MenuItem.String
getActionCommand()
Gets the command name of the action event that is fired by this menu item.ActionListener[]
getActionListeners()
Returns an array of all the action listeners registered on this menu item.String
getLabel()
Gets the label for this menu item.<T extends EventListener>
T[]getListeners(Class<T> listenerType)
Returns an array of all the objects currently registered asFooListener
s upon thisMenuItem
.MenuShortcut
getShortcut()
Get theMenuShortcut
object associated with this menu item,boolean
isEnabled()
Checks whether this menu item is enabled.String
paramString()
Returns a string representing the state of thisMenuItem
.protected void
processActionEvent(ActionEvent e)
Processes action events occurring on this menu item, by dispatching them to any registeredActionListener
objects.protected void
processEvent(AWTEvent e)
Processes events on this menu item.void
removeActionListener(ActionListener l)
Removes the specified action listener so it no longer receives action events from this menu item.void
setActionCommand(String command)
Sets the command name of the action event that is fired by this menu item.void
setEnabled(boolean b)
Sets whether or not this menu item can be chosen.void
setLabel(String label)
Sets the label for this menu item to the specified label.void
setShortcut(MenuShortcut s)
Set theMenuShortcut
object associated with this menu item.-
Methods inherited from class java.awt.MenuComponent
dispatchEvent, getFont, getName, getParent, getPeer, getTreeLock, postEvent, removeNotify, setFont, setName, toString
-
-
-
-
Constructor Detail
-
MenuItem
public MenuItem() throws HeadlessException
Constructs a new MenuItem with an empty label and no keyboard shortcut.- Throws:
HeadlessException
- if GraphicsEnvironment.isHeadless() returns true.- Since:
- JDK1.1
- See Also:
GraphicsEnvironment.isHeadless()
-
MenuItem
public MenuItem(String label) throws HeadlessException
Constructs a new MenuItem with the specified label and no keyboard shortcut. Note that use of "-" in a label is reserved to indicate a separator between menu items. By default, all menu items except for separators are enabled.- Parameters:
label
- the label for this menu item.- Throws:
HeadlessException
- if GraphicsEnvironment.isHeadless() returns true.- Since:
- JDK1.0
- See Also:
GraphicsEnvironment.isHeadless()
-
MenuItem
public MenuItem(String label, MenuShortcut s) throws HeadlessException
Create a menu item with an associated keyboard shortcut. Note that use of "-" in a label is reserved to indicate a separator between menu items. By default, all menu items except for separators are enabled.- Parameters:
label
- the label for this menu item.s
- the instance ofMenuShortcut
associated with this menu item.- Throws:
HeadlessException
- if GraphicsEnvironment.isHeadless() returns true.- Since:
- JDK1.1
- See Also:
GraphicsEnvironment.isHeadless()
-
-
Method Detail
-
addNotify
public void addNotify()
Creates the menu item's peer. The peer allows us to modify the appearance of the menu item without changing its functionality.
-
getLabel
public String getLabel()
Gets the label for this menu item.- Returns:
- the label of this menu item, or
null
if this menu item has no label. - Since:
- JDK1.0
- See Also:
setLabel(java.lang.String)
-
setLabel
public void setLabel(String label)
Sets the label for this menu item to the specified label.- Parameters:
label
- the new label, ornull
for no label.- Since:
- JDK1.0
- See Also:
getLabel()
-
isEnabled
public boolean isEnabled()
Checks whether this menu item is enabled.- Since:
- JDK1.0
- See Also:
setEnabled(boolean)
-
setEnabled
public void setEnabled(boolean b)
Sets whether or not this menu item can be chosen.- Parameters:
b
- iftrue
, enables this menu item; iffalse
, disables it.- Since:
- JDK1.1
- See Also:
isEnabled()
-
enable
@Deprecated public void enable()
Deprecated. As of JDK version 1.1, replaced bysetEnabled(boolean)
.
-
enable
@Deprecated public void enable(boolean b)
Deprecated. As of JDK version 1.1, replaced bysetEnabled(boolean)
.
-
disable
@Deprecated public void disable()
Deprecated. As of JDK version 1.1, replaced bysetEnabled(boolean)
.
-
getShortcut
public MenuShortcut getShortcut()
Get theMenuShortcut
object associated with this menu item,- Returns:
- the menu shortcut associated with this menu item,
or
null
if none has been specified. - Since:
- JDK1.1
- See Also:
setShortcut(java.awt.MenuShortcut)
-
setShortcut
public void setShortcut(MenuShortcut s)
Set theMenuShortcut
object associated with this menu item. If a menu shortcut is already associated with this menu item, it is replaced.- Parameters:
s
- the menu shortcut to associate with this menu item.- Since:
- JDK1.1
- See Also:
getShortcut()
-
deleteShortcut
public void deleteShortcut()
Delete anyMenuShortcut
object associated with this menu item.- Since:
- JDK1.1
-
enableEvents
protected final void enableEvents(long eventsToEnable)
Enables event delivery to this menu item for events to be defined by the specified event mask parameterSince 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 toprocessEvent
regardless of whether a listener is registered.- Parameters:
eventsToEnable
- the event mask defining the event types- Since:
- JDK1.1
- See Also:
processEvent(java.awt.AWTEvent)
,disableEvents(long)
,Component.enableEvents(long)
-
disableEvents
protected final void disableEvents(long eventsToDisable)
Disables event delivery to this menu item for events defined by the specified event mask parameter.- Parameters:
eventsToDisable
- the event mask defining the event types- Since:
- JDK1.1
- See Also:
processEvent(java.awt.AWTEvent)
,enableEvents(long)
,Component.disableEvents(long)
-
setActionCommand
public void setActionCommand(String command)
Sets the command name of the action event that is fired by this menu item.By default, the action command is set to the label of the menu item.
- Parameters:
command
- the action command to be set for this menu item.- Since:
- JDK1.1
- See Also:
getActionCommand()
-
getActionCommand
public String getActionCommand()
Gets the command name of the action event that is fired by this menu item.- Since:
- JDK1.1
- See Also:
setActionCommand(java.lang.String)
-
addActionListener
public void addActionListener(ActionListener l)
Adds the specified action listener to receive action events from this menu item. If l is null, no exception is thrown and no action is performed.Refer to AWT Threading Issues for details on AWT's threading model.
- Parameters:
l
- the action listener.- Since:
- JDK1.1
- See Also:
removeActionListener(java.awt.event.ActionListener)
,getActionListeners()
,ActionEvent
,ActionListener
-
removeActionListener
public void removeActionListener(ActionListener l)
Removes the specified action listener so it no longer receives action events from this menu item. If l is null, no exception is thrown and no action is performed.Refer to AWT Threading Issues for details on AWT's threading model.
- Parameters:
l
- the action listener.- Since:
- JDK1.1
- See Also:
addActionListener(java.awt.event.ActionListener)
,getActionListeners()
,ActionEvent
,ActionListener
-
getActionListeners
public ActionListener[] getActionListeners()
Returns an array of all the action listeners registered on this menu item.- Returns:
- all of this menu item's
ActionListener
s or an empty array if no action listeners are currently registered - Since:
- 1.4
- See Also:
addActionListener(java.awt.event.ActionListener)
,removeActionListener(java.awt.event.ActionListener)
,ActionEvent
,ActionListener
-
getListeners
public <T extends EventListener> T[] getListeners(Class<T> listenerType)
Returns an array of all the objects currently registered asFooListener
s upon thisMenuItem
.FooListener
s are registered using theaddFooListener
method.You can specify the
listenerType
argument with a class literal, such asFooListener.class
. For example, you can query aMenuItem
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.- Parameters:
listenerType
- the type of listeners requested; this parameter should specify an interface that descends fromjava.util.EventListener
- Returns:
- an array of all objects registered as
FooListener
s on this menu item, or an empty array if no such listeners have been added - Throws:
ClassCastException
- iflistenerType
doesn't specify a class or interface that implementsjava.util.EventListener
- Since:
- 1.3
- See Also:
getActionListeners()
-
processEvent
protected void processEvent(AWTEvent e)
Processes events on this menu item. If the event is an instance ofActionEvent
, it invokesprocessActionEvent
, another method defined byMenuItem
.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.- Overrides:
processEvent
in classMenuComponent
- Parameters:
e
- the event- Since:
- JDK1.1
- See Also:
processActionEvent(java.awt.event.ActionEvent)
-
processActionEvent
protected void processActionEvent(ActionEvent e)
Processes action events occurring on this menu item, by dispatching them to any registeredActionListener
objects. This method is not called unless action events are enabled for this component. Action events are enabled when one of the following occurs:- An
ActionListener
object is registered viaaddActionListener
. - Action events are enabled via
enableEvents
.
Note that if the event parameter is
null
the behavior is unspecified and may result in an exception.- Parameters:
e
- the action event- Since:
- JDK1.1
- See Also:
ActionEvent
,ActionListener
,enableEvents(long)
- An
-
paramString
public String paramString()
Returns a string representing the state of thisMenuItem
. 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 benull
.- Overrides:
paramString
in classMenuComponent
- Returns:
- the parameter string of this menu item
-
getAccessibleContext
public AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this MenuItem. For menu items, the AccessibleContext takes the form of an AccessibleAWTMenuItem. A new AccessibleAWTMenuItem instance is created if necessary.- Specified by:
getAccessibleContext
in interfaceAccessible
- Overrides:
getAccessibleContext
in classMenuComponent
- Returns:
- an AccessibleAWTMenuItem that serves as the AccessibleContext of this MenuItem
- Since:
- 1.3
-
-
Traduction non disponible
Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Version en cache
24/11/2024 21:08:29 Cette version de la page est en cache (à la date du 24/11/2024 21:08:29) afin d'accélérer le traitement. Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la dernère version de la page.Document créé le 28/08/2006, dernière modification le 04/03/2020
Source du document imprimé : https://www.gaudry.be/java-api-rf-java/awt/MenuItem.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.