APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing

public Class JMenu

extends JMenuItem
implements Accessible, MenuElement
Class Inheritance
All Implemented Interfaces
javax.swing.MenuElement, javax.accessibility.Accessible
Annotations
@JavaBean
description:A popup window containing menu items displayed in a menu bar.
@SwingContainer
@SuppressWarnings:serial
Imports
java.awt.Component, .ComponentOrientation, .Container, .Dimension, .GraphicsConfiguration, .GraphicsDevice, .GraphicsEnvironment, .Insets, .Point, .Rectangle, .Toolkit, java.awt.event.KeyEvent, .WindowAdapter, .WindowEvent, java.beans.BeanProperty, .JavaBean, .PropertyChangeListener, java.io.IOException, .ObjectOutputStream, .Serial, .Serializable, java.util.ArrayList, javax.accessibility.Accessible, .AccessibleContext, .AccessibleRole, .AccessibleSelection, javax.swing.event.ChangeEvent, .ChangeListener, .EventListenerList, .MenuEvent, .MenuListener, javax.swing.plaf.MenuItemUI, .PopupMenuUI

An implementation of a menu -- a popup window containing JMenuItems that is displayed when the user selects an item on the JMenuBar. In addition to JMenuItems, a JMenu can also contain JSeparators.

In essence, a menu is a button with an associated JPopupMenu. When the "button" is pressed, the JPopupMenu appears. If the "button" is on the JMenuBar, the menu is a top-level window. If the "button" is another menu item, then the JPopupMenu is "pull-right" menu.

Menus can be configured, and to some degree controlled, by Actions. Using an Action with a menu has many benefits beyond directly configuring a menu. Refer to Swing Components Supporting Action for more details, and you can find more information in How to Use Actions, a section in The Java Tutorial.

For information and examples of using menus see How to Use Menus, a section in The Java Tutorial.

Warning

Swing is not thread safe. For more information see Swing's Threading Policy.

Warning

Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans package. Please see java.beans.XMLEncoder.

Authors
Georges Saab, David Karlton, Arnaud Weber
Since
1.2
See Also
JMenuItem, JSeparator, JMenuBar, JPopupMenu

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
JMenu.AccessibleJMenu

This class implements accessibility support for the JMenu class.

protected class
JMenu.WinListener

A listener class that watches for a popup window closing.

Field Summary

Modifier and TypeField and Description
private Point
private static final boolean
private int
protected JMenu.WinListener
popupListener

The window-closing listener for the popup.

private JPopupMenu
private static final boolean
private static final String
private static final boolean

Constructor Summary

AccessConstructor and Description
public
JMenu()

Constructs a new JMenu with no text.

public
JMenu(String
the text for the menu label
s
)

Constructs a new JMenu with the supplied string as its text.

public
JMenu(Action
an Action
a
)

Constructs a menu whose properties are taken from the Action supplied.

public
JMenu(String
the text for the menu label
s
,
boolean
can the menu be torn off (not yet implemented)
b
)

Constructs a new JMenu with the supplied string as its text and specified as a tear-off menu or not.

Method Summary

Modifier and TypeMethod and Description
public JMenuItem

Returns:

the JMenuItem added
add
(JMenuItem
the JMenuitem to be added
menuItem
)

Appends a menu item to the end of this menu.

public Component

Returns:

the Component added
add
(Component
the Component to add
c
)

Overrides java.awt.Container.add.

Appends a component to the end of this menu.

public Component

Returns:

the Component added
add
(Component
the Component to add
c
,
int
the position at which to insert the component
index
)

Overrides java.awt.Container.add.

Adds the specified component to this container at the given position.

public JMenuItem

Returns:

the new JMenuItem
add
(String
the string for the menu item to be added
s
)

Creates a new menu item with the specified text and appends it to the end of this menu.

public JMenuItem

Returns:

the new JMenuItem
add
(Action
the Action for the menu item to be added
a
)

Creates a new menu item attached to the specified Action object and appends it to the end of this menu.

public void
addMenuListener(MenuListener
the listener to be added
l
)

Adds a listener for menu events.

public void
addSeparator()

Appends a new separator to the end of the menu.

public void
applyComponentOrientation(ComponentOrientation
the new component orientation of this menu and the components contained within it.
o
)

Overrides java.awt.Container.applyComponentOrientation.

Sets the ComponentOrientation property of this menu and all components contained within it.

private MenuElement[]
pack-priv void
protected PropertyChangeListener

Returns:

a PropertyChangeListener for b
createActionChangeListener
(JMenuItem
a menu item for which to create a PropertyChangeListener
b
)

Returns a properly configured PropertyChangeListener which updates the control as changes to the Action occur.

protected JMenuItem

Returns:

the new menu item
createActionComponent
(Action
the Action for the menu item to be added
a
)

Factory method which creates the JMenuItem for Actions added to the JMenu.

private ChangeListener
protected JMenu.WinListener

Returns:

the new window-closing listener
createWinListener
(JPopupMenu
the JPopupMenu
p
)

Creates a window-closing listener for the popup.

public void
doClick(int
indicates the number of milliseconds the button was pressed for
pressTime
)

Overrides javax.swing.AbstractButton.doClick.

Programmatically performs a "click".

private void
protected void
fireMenuCanceled()

Notifies all listeners that have registered interest for notification on this event type.

protected void
fireMenuDeselected()

Notifies all listeners that have registered interest for notification on this event type.

protected void
fireMenuSelected()

Notifies all listeners that have registered interest for notification on this event type.

public AccessibleContext

Returns:

an AccessibleJMenu that serves as the AccessibleContext of this JMenu
getAccessibleContext
()

Overrides javax.swing.JMenuItem.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this JMenu.

public Component
getComponent()

Overrides javax.swing.JMenuItem.getComponent.

Implements javax.swing.MenuElement.getComponent.

Returns the java.awt.Component used to paint this MenuElement.

private Point
public int

Returns:

the delay property
getDelay
()

Returns the suggested delay, in milliseconds, before submenus are popped up or down.

public JMenuItem

Returns:

the menu item at the specified position; or null if the item as the specified position is not a menu item
getItem
(int
an integer specifying the position
pos
)

Returns the JMenuItem at the specified position.

public int

Returns:

an integer equal to the number of items on the menu
getItemCount
()

Returns the number of items on the menu, including separators.

public Component

Returns:

the component requested, or null if there is no popup menu
getMenuComponent
(int
the position of the component to be returned
n
)

Returns the component at position n.

public int

Returns:

an integer containing the number of components on the menu
getMenuComponentCount
()

Returns the number of components on the menu.

public Component[]

Returns:

an array of Components or an empty array if there is no popup menu
getMenuComponents
()

Returns an array of Components of the menu's subcomponents.

public MenuListener[]

Returns:

all of the MenuListeners added or an empty array if no listeners have been added
getMenuListeners
()

Returns an array of all the MenuListeners added to this JMenu with addMenuListener().

public JPopupMenu

Returns:

the JPopupMenu associated with this menu
getPopupMenu
()

Returns the popupmenu associated with this menu.

protected Point

Returns:

a Point in the coordinate space of the menu which should be used as the origin of the JMenu's popup menu
getPopupMenuOrigin
()

Computes the origin for the JMenu's popup menu.

public MenuElement[]

Returns:

an array of MenuElement objects
getSubElements
()

Overrides javax.swing.JMenuItem.getSubElements.

Implements javax.swing.MenuElement.getSubElements.

Returns an array of MenuElements containing the submenu for this menu component.

public String

Returns:

the string "MenuUI"
getUIClassID
()

Overrides javax.swing.JMenuItem.getUIClassID.

Returns the name of the L&F class that renders this component.

pack-priv void
initFocusability()

Overrides javax.swing.JMenuItem.initFocusability.

Overridden to do nothing.

public void
insert(String
the text for the menu item to add
s
,
int
an integer specifying the position at which to add the new menu item
pos
)

Inserts a new menu item with the specified text at a given position.

public JMenuItem

Returns:

the new menu item
insert
(JMenuItem
the JMenuitem to add
mi
,
int
an integer specifying the position at which to add the new JMenuitem
pos
)

Inserts the specified JMenuitem at a given position.

public JMenuItem

Returns:

the new menu item
insert
(Action
the Action object for the menu item to add
a
,
int
an integer specifying the position at which to add the new menu item
pos
)

Inserts a new menu item attached to the specified Action object at a given position.

public void
insertSeparator(int
an integer specifying the position at which to insert the menu separator
index
)

Inserts a separator at the specified position.

public boolean

Returns:

true if the Component exists, false otherwise
isMenuComponent
(Component
the Component to be tested
c
)

Returns true if the specified component exists in the submenu hierarchy.

public boolean

Returns:

true if the menu is visible, else false
isPopupMenuVisible
()

Returns true if the menu's popup window is visible.

public boolean

Returns:

true if the menu is selected, else false
isSelected
()

Overrides javax.swing.AbstractButton.isSelected.

Returns true if the menu is currently selected (highlighted).

public boolean

Returns:

true if the menu can be torn off, else false
isTearOff
()

Returns true if the menu can be torn off.

public boolean

Returns:

true if the menu is activated from the menu bar; false if the menu is activated from a menu item on another menu
isTopLevelMenu
()

Returns true if the menu is a 'top-level menu', that is, if it is the direct child of a menubar.

protected String

Returns:

a string representation of this JMenu.
paramString
()

Overrides javax.swing.JMenuItem.paramString.

Returns a string representation of this JMenu.

protected void
processKeyEvent(KeyEvent
the key event to be processed
evt
)

Overrides javax.swing.JComponent.processKeyEvent.

Processes key stroke events such as mnemonics and accelerators.

public void
remove(JMenuItem
the JMenuItem to be removed from the menu
item
)

Removes the specified menu item from this menu.

public void
remove(int
the position of the item to be removed
pos
)

Overrides java.awt.Container.remove.

Removes the menu item at the specified index from this menu.

public void
remove(Component
the component to be removed
c
)

Overrides java.awt.Container.remove.

Removes the component c from this menu.

public void
removeAll()

Overrides java.awt.Container.removeAll.

Removes all menu items from this menu.

public void
removeMenuListener(MenuListener
the listener to be removed
l
)

Removes a listener for menu events.

public void
setAccelerator(KeyStroke
the keystroke combination which will invoke the JMenuItem's actionlisteners without navigating the menu hierarchy
keyStroke
)

Overrides javax.swing.JMenuItem.setAccelerator.

setAccelerator is not defined for JMenu.

public void
setComponentOrientation(ComponentOrientation
the new orientation for this menu and its associated popup menu.
o
)

Overrides java.awt.Component.setComponentOrientation.

Sets the orientation for this menu and its associated popup menu determined by the ComponentOrientation argument.

public void
setDelay(int
the number of milliseconds to delay
d
)

Sets the suggested delay before the menu's PopupMenu is popped up or down.

public void
setMenuLocation(int
the x coordinate of the popup's new position
x
,
int
the y coordinate of the popup's new position
y
)

Sets the location of the popup component.

public void
setModel(ButtonModel
the ButtonModel
newModel
)

Overrides javax.swing.JMenuItem.setModel.

Sets the data model for the "menu button" -- the label that the user clicks to open or close the menu.

public void
setPopupMenuVisible(boolean
a boolean value -- true to make the menu visible, false to hide it
b
)

Sets the visibility of the menu's popup.

public void
setSelected(boolean
true to select (highlight) the menu; false to de-select the menu
b
)

Overrides javax.swing.AbstractButton.setSelected.

Sets the selection status of the menu.

private Point
private Point
translateToPopupMenu(int x, int y)

public void
updateUI()

Overrides javax.swing.JMenuItem.updateUI.

Resets the UI property with a value from the current look and feel.

private void
writeObject(ObjectOutputStream s)

Hides javax.swing.JMenuItem.writeObject.

See readObject and writeObject in JComponent for more information about serialization in Swing.

Inherited from javax.swing.JMenuItem:
actionPropertyChangedaddMenuDragMouseListeneraddMenuKeyListeneralwaysOnTopconfigurePropertiesFromActionfireMenuDragMouseDraggedfireMenuDragMouseEnteredfireMenuDragMouseExitedfireMenuDragMouseReleasedfireMenuKeyPressedfireMenuKeyReleasedfireMenuKeyTypedgetAcceleratorgetMenuDragMouseListenersgetMenuKeyListenersinitisArmedlargeIconChangedprocessKeyEventprocessMenuDragMouseEventprocessMenuKeyEventprocessMouseEventremoveMenuDragMouseListenerremoveMenuKeyListenersetArmedsetEnabledsetIconFromActionsetUIsmallIconChanged