javax.swing

Class JMenu

public class JMenu extends JMenuItem implements Accessible, MenuElement

This class represents a menu that can be added to a menu bar or can be a submenu in some other menu. When JMenu is selected it displays JPopupMenu containing its menu items.

JMenu's fires MenuEvents when this menu's selection changes. If this menu is selected, then fireMenuSelectedEvent() is invoked. In case when menu is deselected or cancelled, then fireMenuDeselectedEvent() or fireMenuCancelledEvent() is invoked, respectivelly.

Nested Class Summary
protected classJMenu.AccessibleJMenu
Implements support for assisitive technologies for JMenu.
protected classJMenu.WinListener
Field Summary
protected JMenu.WinListenerpopupListener
Constructor Summary
JMenu()
Creates a new JMenu object.
JMenu(String text)
Creates a new JMenu with the specified label.
JMenu(Action action)
Creates a new JMenu object.
JMenu(String text, boolean tearoff)
Creates a new JMenu with specified label and an option for this menu to be tear-off menu.
Method Summary
JMenuItemadd(JMenuItem item)
Adds specified menu item to this menu
Componentadd(Component component)
Adds specified component to this menu.
Componentadd(Component component, int index)
Adds specified component to this menu at the given index
JMenuItemadd(String text)
Adds JMenuItem constructed with the specified label to this menu
JMenuItemadd(Action action)
Adds JMenuItem constructed using properties from specified action.
voidaddMenuListener(MenuListener listener)
Adds MenuListener to the menu
voidaddSeparator()
Adds separator to the end of the menu items in the menu.
protected PropertyChangeListenercreateActionChangeListener(JMenuItem item)
Creates ActionChangeListener to listen for PropertyChangeEvents occuring in the action that is associated with this menu
protected JMenuItemcreateActionComponent(Action action)
Creates and returns JMenuItem associated with the given action
protected JMenu.WinListenercreateWinListener(JPopupMenu popup)
Creates WinListener that listens to the menu;s popup menu.
voiddoClick(int time)
Programatically performs click
protected voidfireMenuCanceled()
This method fires MenuEvents to all menu's MenuListeners.
protected voidfireMenuDeselected()
This method fires MenuEvents to all menu's MenuListeners.
protected voidfireMenuSelected()
This method fires MenuEvents to all menu's MenuListeners.
AccessibleContextgetAccessibleContext()
ComponentgetComponent()
intgetDelay()
Returns delay property.
JMenuItemgetItem(int index)
Returns menu item located at the specified index in the menu
intgetItemCount()
Returns number of items in the menu including separators.
ComponentgetMenuComponent(int index)
Returns menu component located at the givent index in the menu
intgetMenuComponentCount()
Returns number of menu components in this menu
Component[]getMenuComponents()
Return components belonging to this menu
MenuListener[]getMenuListeners()
Returns all registered MenuListener objects.
JPopupMenugetPopupMenu()
Returns popup menu associated with the menu.
protected PointgetPopupMenuOrigin()
Returns origin point of the popup menu.
MenuElement[]getSubElements()
Method of MenuElement interface.
StringgetUIClassID()
This method returns a name to identify which look and feel class will be the UI delegate for the menu.
voidinsert(String text, int index)
Creates JMenuItem with the specified text and inserts it in the at the specified index
JMenuIteminsert(JMenuItem item, int index)
Creates JMenuItem with the specified text and inserts it in the at the specified index.
JMenuIteminsert(Action action, int index)
Creates JMenuItem with the associated action and inserts it to the menu at the specified index.
voidinsertSeparator(int index)
Inserts separator in the menu at the specified index.
booleanisMenuComponent(Component component)
Checks if given component exists in this menu.
booleanisPopupMenuVisible()
Checks if PopupMenu associated with this menu is visible
booleanisSelected()
Returns true if the menu is selected and false otherwise
booleanisTearOff()
Checks if this menu is a tear-off menu.
booleanisTopLevelMenu()
Checks if this menu is a top level menu.
voidmenuSelectionChanged(boolean changed)
Method of the MenuElementInterface.
protected StringparamString()
A string that describes this JMenu.
protected voidprocessKeyEvent(KeyEvent event)
This method process KeyEvent occuring when the menu is visible
voidremove(JMenuItem item)
Removes given menu item from this menu.
voidremove(int index)
Removes component at the specified index from this menu
voidremove(Component component)
Removes given component from this menu.
voidremoveAll()
Removes all menu items from the menu
voidremoveMenuListener(MenuListener listener)
Removes MenuListener from the menu
voidsetAccelerator(KeyStroke keystroke)
This method is overriden with empty implementation, s.t the accelerator couldn't be set for the menu.
voidsetDelay(int delay)
Sets delay property for this menu.
voidsetMenuLocation(int x, int y)
Sets location at which popup menu should be displayed The location given is relative to this menu item
voidsetModel(ButtonModel model)
Sets model for this menu.
voidsetPopupMenuVisible(boolean popup)
Sets popup menu visibility
voidsetSelected(boolean selected)
Changes this menu selected state if selected is true and false otherwise This method fires menuEvents to menu's registered listeners.
voidupdateUI()
This method sets this menuItem's UI to the UIManager's default for the current look and feel.

Field Detail

popupListener

protected JMenu.WinListener popupListener

Constructor Detail

JMenu

public JMenu()
Creates a new JMenu object.

JMenu

public JMenu(String text)
Creates a new JMenu with the specified label.

Parameters: text label for this menu

JMenu

public JMenu(Action action)
Creates a new JMenu object.

Parameters: action Action that is used to create menu item tha will be added to the menu.

JMenu

public JMenu(String text, boolean tearoff)
Creates a new JMenu with specified label and an option for this menu to be tear-off menu.

Parameters: text label for this menu tearoff true if this menu should be tear-off and false otherwise

Method Detail

add

public JMenuItem add(JMenuItem item)
Adds specified menu item to this menu

Parameters: item Menu item to add to this menu

Returns: Menu item that was added

add

public Component add(Component component)
Adds specified component to this menu.

Parameters: component Component to add to this menu

Returns: Component that was added

add

public Component add(Component component, int index)
Adds specified component to this menu at the given index

Parameters: component Component to add index Position of this menu item in the menu

Returns: Component that was added

add

public JMenuItem add(String text)
Adds JMenuItem constructed with the specified label to this menu

Parameters: text label for the menu item that will be added

Returns: Menu Item that was added to this menu

add

public JMenuItem add(Action action)
Adds JMenuItem constructed using properties from specified action.

Parameters: action action to construct the menu item with

Returns: Menu Item that was added to this menu

addMenuListener

public void addMenuListener(MenuListener listener)
Adds MenuListener to the menu

Parameters: listener MenuListener to add

addSeparator

public void addSeparator()
Adds separator to the end of the menu items in the menu.

createActionChangeListener

protected PropertyChangeListener createActionChangeListener(JMenuItem item)
Creates ActionChangeListener to listen for PropertyChangeEvents occuring in the action that is associated with this menu

Parameters: item menu that contains action to listen to

Returns: The PropertyChangeListener

createActionComponent

protected JMenuItem createActionComponent(Action action)
Creates and returns JMenuItem associated with the given action

Parameters: action Action to use for creation of JMenuItem

Returns: JMenuItem that was creted with given action

createWinListener

protected JMenu.WinListener createWinListener(JPopupMenu popup)
Creates WinListener that listens to the menu;s popup menu.

Parameters: popup JPopupMenu to listen to

Returns: The WinListener

doClick

public void doClick(int time)
Programatically performs click

Parameters: time Number of milliseconds for which this menu stays pressed

fireMenuCanceled

protected void fireMenuCanceled()
This method fires MenuEvents to all menu's MenuListeners. In this case menuSelected() method of MenuListeners is called to indicated that the menu was cancelled. The menu is cancelled when it's popup menu is close without selection.

fireMenuDeselected

protected void fireMenuDeselected()
This method fires MenuEvents to all menu's MenuListeners. In this case menuDeselected() method of MenuListeners is called to indicated that the menu was deselected.

fireMenuSelected

protected void fireMenuSelected()
This method fires MenuEvents to all menu's MenuListeners. In this case menuSelected() method of MenuListeners is called to indicated that the menu was selected.

getAccessibleContext

public AccessibleContext getAccessibleContext()

getComponent

public Component getComponent()

Returns: Returns reference to itself

getDelay

public int getDelay()
Returns delay property.

Returns: delay property, indicating number of milliseconds before popup menu associated with the menu appears or disappears after menu was selected or deselected respectively

getItem

public JMenuItem getItem(int index)
Returns menu item located at the specified index in the menu

Parameters: index Index at which to look for the menu item

Returns: menu item located at the specified index in the menu

getItemCount

public int getItemCount()
Returns number of items in the menu including separators.

Returns: number of items in the menu

See Also: getMenuComponentCount

getMenuComponent

public Component getMenuComponent(int index)
Returns menu component located at the givent index in the menu

Parameters: index index at which to get the menu component in the menu

Returns: Menu Component located in the menu at the specified index

getMenuComponentCount

public int getMenuComponentCount()
Returns number of menu components in this menu

Returns: number of menu components in this menu

getMenuComponents

public Component[] getMenuComponents()
Return components belonging to this menu

Returns: components belonging to this menu

getMenuListeners

public MenuListener[] getMenuListeners()
Returns all registered MenuListener objects.

Returns: an array of listeners

Since: 1.4

getPopupMenu

public JPopupMenu getPopupMenu()
Returns popup menu associated with the menu.

Returns: popup menu associated with the menu.

getPopupMenuOrigin

protected Point getPopupMenuOrigin()
Returns origin point of the popup menu. This takes the screen bounds into account and places the popup where it fits best.

Returns: the origin of the popup menu

getSubElements

public MenuElement[] getSubElements()
Method of MenuElement interface. Returns sub components of this menu.

Returns: array containing popupMenu that is associated with this menu

getUIClassID

public String getUIClassID()
This method returns a name to identify which look and feel class will be the UI delegate for the menu.

Returns: The Look and Feel classID. "MenuUI"

insert

public void insert(String text, int index)
Creates JMenuItem with the specified text and inserts it in the at the specified index

Parameters: text label for the new menu item index index at which to insert newly created menu item.

insert

public JMenuItem insert(JMenuItem item, int index)
Creates JMenuItem with the specified text and inserts it in the at the specified index. IllegalArgumentException is thrown if index is less than 0

Parameters: item menu item to insert index index at which to insert menu item.

Returns: Menu item that was added to the menu

insert

public JMenuItem insert(Action action, int index)
Creates JMenuItem with the associated action and inserts it to the menu at the specified index. IllegalArgumentException is thrown if index is less than 0

Parameters: action Action for the new menu item index index at which to insert newly created menu item.

Returns: Menu item that was added to the menu

insertSeparator

public void insertSeparator(int index)
Inserts separator in the menu at the specified index.

Parameters: index Index at which separator should be inserted

isMenuComponent

public boolean isMenuComponent(Component component)
Checks if given component exists in this menu. The submenus of this menu are checked as well

Parameters: component Component to look for

Returns: true if the given component exists in this menu, and false otherwise

isPopupMenuVisible

public boolean isPopupMenuVisible()
Checks if PopupMenu associated with this menu is visible

Returns: true if the popup associated with this menu is currently visible on the screen and false otherwise.

isSelected

public boolean isSelected()
Returns true if the menu is selected and false otherwise

Returns: true if the menu is selected and false otherwise

isTearOff

public boolean isTearOff()
Checks if this menu is a tear-off menu.

Returns: true if this menu is a tear-off menu and false otherwise

isTopLevelMenu

public boolean isTopLevelMenu()
Checks if this menu is a top level menu. The menu is top level menu if it is inside the menu bar. While if the menu inside some other menu, it is considered to be a pull-right menu.

Returns: true if this menu is top level menu, and false otherwise

menuSelectionChanged

public void menuSelectionChanged(boolean changed)
Method of the MenuElementInterface. It reacts to the selection changes in the menu. If this menu was selected, then it displayes popup menu associated with it and if this menu was deselected it hides the popup menu.

Parameters: changed true if the menu was selected and false otherwise

paramString

protected String paramString()
A string that describes this JMenu. Normally only used for debugging.

Returns: A string describing this JMenu

processKeyEvent

protected void processKeyEvent(KeyEvent event)
This method process KeyEvent occuring when the menu is visible

Parameters: event The KeyEvent

remove

public void remove(JMenuItem item)
Removes given menu item from this menu. Nothing happens if this menu doesn't contain specified menu item.

Parameters: item Menu Item which needs to be removed

remove

public void remove(int index)
Removes component at the specified index from this menu

Parameters: index Position of the component that needs to be removed in the menu

remove

public void remove(Component component)
Removes given component from this menu.

Parameters: component Component to remove

removeAll

public void removeAll()
Removes all menu items from the menu

removeMenuListener

public void removeMenuListener(MenuListener listener)
Removes MenuListener from the menu

Parameters: listener MenuListener to remove

setAccelerator

public void setAccelerator(KeyStroke keystroke)
This method is overriden with empty implementation, s.t the accelerator couldn't be set for the menu. The mnemonic should be used for the menu instead.

Parameters: keystroke accelerator for this menu

setDelay

public void setDelay(int delay)
Sets delay property for this menu. If given time for the delay property is negative, then IllegalArgumentException is thrown

Parameters: delay number of milliseconds before popup menu associated with the menu appears or disappears after menu was selected or deselected respectively

setMenuLocation

public void setMenuLocation(int x, int y)
Sets location at which popup menu should be displayed The location given is relative to this menu item

Parameters: x x-coordinate of the menu location y y-coordinate of the menu location

setModel

public void setModel(ButtonModel model)
Sets model for this menu.

Parameters: model model to set

setPopupMenuVisible

public void setPopupMenuVisible(boolean popup)
Sets popup menu visibility

Parameters: popup true if popup should be visible and false otherwise

setSelected

public void setSelected(boolean selected)
Changes this menu selected state if selected is true and false otherwise This method fires menuEvents to menu's registered listeners.

Parameters: selected true if the menu should be selected and false otherwise

updateUI

public void updateUI()
This method sets this menuItem's UI to the UIManager's default for the current look and feel.