JMenuItem represents element in the menu. It inherits most of
its functionality from AbstractButton, however its behavior somewhat
varies from it. JMenuItem fire different kinds of events.
PropertyChangeEvents are fired when menuItems properties are modified;
ChangeEvents are fired when menuItem's state changes and actionEvents are
fired when menu item is selected. In addition to this events menuItem also
fire MenuDragMouseEvent and MenuKeyEvents when mouse is dragged over
the menu item or associated key with menu item is invoked respectively.
JMenuItem
public JMenuItem()
Creates a new JMenuItem object.
JMenuItem
public JMenuItem(String text)
Creates a new JMenuItem with the given label.
text
- label for the menu item
JMenuItem
public JMenuItem(String text,
int mnemonic)
Creates a new JMenuItem object.
text
- label for this menu itemmnemonic
- - Single key that can be used with a
look-and-feel meta key to activate this menu item. However
menu item should be visible on the screen when mnemonic is used.
JMenuItem
public JMenuItem(String text,
Icon icon)
Creates a new JMenuItem with specified text and icon.
Text is displayed to the left of icon by default.
text
- label for this menu itemicon
- icon that will be displayed on this menu item
JMenuItem
public JMenuItem(Action action)
Creates a new JMenuItem associated with the specified action.
action
- action for this menu item
JMenuItem
public JMenuItem(Icon icon)
Creates a new JMenuItem with the given icon.
icon
- Icon that will be displayed on the menu item
addMenuDragMouseListener
public void addMenuDragMouseListener(MenuDragMouseListener listener)
Adds a MenuDragMouseListener to this menu item. When mouse
is dragged over the menu item the MenuDragMouseEvents will be
fired, and these listeners will be called.
listener
- The new listener to add
addMenuKeyListener
public void addMenuKeyListener(MenuKeyListener listener)
Adds an MenuKeyListener to this menu item. This listener will be
invoked when MenuKeyEvents will be fired by this menu item.
listener
- The new listener to add
fireMenuDragMouseDragged
protected void fireMenuDragMouseDragged(MenuDragMouseEvent event)
Fires MenuDragMouseEvent to all of the menuItem's MouseInputListeners.
event
- The event signifying that mouse is being dragged over the menuItem
fireMenuDragMouseEntered
protected void fireMenuDragMouseEntered(MenuDragMouseEvent event)
Fires MenuDragMouseEvent to all of the menuItem's MouseInputListeners.
event
- The event signifying that mouse entered menuItem while it was dragged
fireMenuDragMouseExited
protected void fireMenuDragMouseExited(MenuDragMouseEvent event)
Fires MenuDragMouseEvent to all of the menuItem's MouseInputListeners.
event
- The event signifying that mouse has exited menu item, while it was dragged
fireMenuDragMouseReleased
protected void fireMenuDragMouseReleased(MenuDragMouseEvent event)
This method fires a MenuDragMouseEvent to all the MenuItem's MouseInputListeners.
event
- The event signifying that mouse was released while it was dragged over the menuItem
fireMenuKeyPressed
protected void fireMenuKeyPressed(MenuKeyEvent event)
This method fires a MenuKeyEvent to all the MenuItem's MenuKeyListeners.
event
- The event signifying that key associated with this menu was pressed
fireMenuKeyReleased
protected void fireMenuKeyReleased(MenuKeyEvent event)
This method fires a MenuKeyEvent to all the MenuItem's MenuKeyListeners.
event
- The event signifying that key associated with this menu was released
fireMenuKeyTyped
protected void fireMenuKeyTyped(MenuKeyEvent event)
This method fires a MenuKeyEvent to all the MenuItem's MenuKeyListeners.
event
- The event signifying that key associated with this menu was typed.
The key is typed when it was pressed and then released
getAccelerator
public KeyStroke getAccelerator()
Return accelerator for this menu item.
- $KeyStroke$ accelerator for this menu item.
getComponent
public Component getComponent()
Returns reference to the component that will paint this menu item.
- getComponent in interface MenuElement
- $Component$ Component that will paint this menu item.
Simply returns reference to this menu item.
getSubElements
public MenuElement[] getSubElements()
Method of the MenuElement interface.
- getSubElements in interface MenuElement
- $MenuElement[]$ Returns array of sub-components for this menu
item. By default menuItem doesn't have any subcomponents and so
empty array is returned instead.
getUIClassID
public String getUIClassID()
This method returns a name to identify which look and feel class will be
the UI delegate for the menuItem.
- getUIClassID in interface JComponent
- The Look and Feel classID. "MenuItemUI"
init
protected void init(String text,
Icon icon)
Initializes this menu item
- init in interface AbstractButton
text
- label for this menu itemicon
- icon to be displayed for this menu item
isArmed
public boolean isArmed()
Returns true if button's model is armed and false otherwise. The
button model is armed if menu item has focus or it is selected.
- $boolean$ true if button's model is armed and false otherwise
menuSelectionChanged
public void menuSelectionChanged(boolean changed)
Method of the MenuElement interface.
This method is invoked by MenuSelectionManager when selection of
this menu item has changed. If this menu item was selected then
arm it's model, and disarm the model otherwise. The menu item
is considered to be selected, and thus highlighted when its model
is armed.
- menuSelectionChanged in interface MenuElement
changed
- indicates selection status of this menu item. If changed is
true then menu item is selected and deselected otherwise.
processMenuDragMouseEvent
public void processMenuDragMouseEvent(MenuDragMouseEvent event)
This method fires MenuDragMouseEvents to registered listeners.
Different types of MenuDragMouseEvents are fired depending
on the observed mouse event.
processMenuKeyEvent
public void processMenuKeyEvent(MenuKeyEvent event)
This method fires MenuKeyEvent to registered listeners.
Different types of MenuKeyEvents are fired depending
on the observed key event.
setAccelerator
public void setAccelerator(KeyStroke keystroke)
Sets the key combination which invokes the menu item's action
listeners without navigating the menu hierarchy. Note that when the
keyboard accelerator is typed, it will work whether or not the
menu is currently displayed.
keystroke
- accelerator for this menu item.
setArmed
public void setArmed(boolean armed)
Sets menuItem's "ARMED" property
setEnabled
public void setEnabled(boolean enabled)
Enable or disable menu item. When menu item is disabled,
its text and icon are grayed out if they exist.
- setEnabled in interface AbstractButton
enabled
- if true enable menu item, and disable otherwise.
setUI
public void setUI(MenuItemUI ui)
Set the "UI" property of the menu item, which is a look and feel class
responsible for handling menuItem's input events and painting it.
ui
- The new "UI" property
updateUI
public void updateUI()
This method sets this menuItem's UI to the UIManager's default for the
current look and feel.
- updateUI in interface AbstractButton
JMenuItem.java --
Copyright (C) 2002, 2004, 2005,2006 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.