javax.swing
Class JMenu

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--javax.swing.JComponent
                    |
                    +--javax.swing.AbstractButton
                          |
                          +--javax.swing.JMenuItem
                                |
                                +--javax.swing.JMenu

public class JMenu
extends javax.swing.JMenuItem
implements javax.accessibility.Accessible, javax.swing.MenuElement

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.

For the keyboard keys used by this component in the standard Look and Feel (L&F) renditions, see the JMenu key assignments.

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. A future release of Swing will provide support for long term persistence.

See Also:
JMenuItem, JSeparator, JMenuBar, JPopupMenu, Serialized Form

Inner classes inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Fields inherited from class javax.swing.AbstractButton
BORDER_PAINTED_CHANGED_PROPERTY, CONTENT_AREA_FILLED_CHANGED_PROPERTY, DISABLED_ICON_CHANGED_PROPERTY, DISABLED_SELECTED_ICON_CHANGED_PROPERTY, FOCUS_PAINTED_CHANGED_PROPERTY, HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY, HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY, ICON_CHANGED_PROPERTY, MARGIN_CHANGED_PROPERTY, MNEMONIC_CHANGED_PROPERTY, MODEL_CHANGED_PROPERTY, PRESSED_ICON_CHANGED_PROPERTY, ROLLOVER_ENABLED_CHANGED_PROPERTY, ROLLOVER_ICON_CHANGED_PROPERTY, ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY, SELECTED_ICON_CHANGED_PROPERTY, TEXT_CHANGED_PROPERTY, VERTICAL_ALIGNMENT_CHANGED_PROPERTY, VERTICAL_TEXT_POSITION_CHANGED_PROPERTY
 
Fields inherited from class javax.swing.JComponent
TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Constructor Summary
JMenu()
          Creates a new JMenu with no text.
JMenu(java.lang.String s)
          Creates a new JMenu with the supplied string as its text
JMenu(java.lang.String s, boolean b)
          Creates a new JMenu with the supplied string as its text and specified as a tear-off menu or not.
 
Method Summary
 javax.swing.JMenuItem add(javax.swing.Action a)
          Creates a new menuitem attached to the specified Action object and appends it to the end of this menu.
 java.awt.Component add(java.awt.Component c)
          Appends a component to the end of this menu.
 javax.swing.JMenuItem add(javax.swing.JMenuItem menuItem)
          Appends a menuitem to the end of this menu.
 javax.swing.JMenuItem add(java.lang.String s)
          Creates a new menuitem with the specified text and appends it to the end of this menu.
 void addMenuListener(javax.swing.event.MenuListener l)
          Add a listener for menu events
 void addSeparator()
          Append a new separator to the end of the menu.
 void doClick(int pressTime)
          Programatically perform a "click".
 javax.accessibility.AccessibleContext getAccessibleContext()
          Get the AccessibleContext associated with this JComponent
 java.awt.Component getComponent()
          This method returns the java.awt.Component used to paint this MenuElement.
 int getDelay()
          Returns the suggested delay before the menu's PopupMenu is popped up or down.
 javax.swing.JMenuItem getItem(int pos)
          Returns the JMenuItem at the specified position.
 int getItemCount()
          Returns the number of items on the menu, including separators.
 java.awt.Component getMenuComponent(int n)
          Returns the component at position n
 int getMenuComponentCount()
          Returns the number of components on the menu.
 java.awt.Component[] getMenuComponents()
          Returns an array of the menu's subcomponents
 javax.swing.JPopupMenu getPopupMenu()
          Returns the popupmenu associated with this menu
 javax.swing.MenuElement[] getSubElements()
          Returns an array containing the sub-menu components for this menu component
 java.lang.String getUIClassID()
          Returns the name of the L&F class that renders this component.
 javax.swing.JMenuItem insert(javax.swing.Action a, int pos)
          Insert a new menuitem attached to the specified Action object at a given position.
 javax.swing.JMenuItem insert(javax.swing.JMenuItem mi, int pos)
          Insert the specified JMenuitem at a given position.
 void insert(java.lang.String s, int pos)
          Insert a new menuitem with the specified text at a given position.
 void insertSeparator(int index)
          Inserts a separator at the specified position.
 boolean isMenuComponent(java.awt.Component c)
          Returns true if the specified component exists in the submenu hierarchy.
 boolean isPopupMenuVisible()
          Returns true if the menu's popup window is visible.
 boolean isSelected()
          Returns true if the menu is currently selected (popped up).
 boolean isTearOff()
          Returns true if the menu can be torn off.
 boolean isTopLevelMenu()
          Returns true if the menu is a 'top-level menu', that is, if it is the direct child of a menubar.
 void menuSelectionChanged(boolean isIncluded)
          Messaged when the menubar selection changes to activate or deactivate this menu.
 void remove(java.awt.Component c)
          Removes the Component from this menu.
 void remove(int pos)
          Removes the menu item at the specified index from this menu.
 void remove(javax.swing.JMenuItem item)
          Removes the specified menu item from this menu.
 void removeAll()
          Remove all menu items from this menu.
 void removeMenuListener(javax.swing.event.MenuListener l)
          Remove a listener for menu events
 void setAccelerator(javax.swing.KeyStroke keyStroke)
          setAccelerator() is not defined for JMenu.
 void setDelay(int d)
          Sets the suggested delay before the menu's PopupMenu is popped up or down.
 void setMenuLocation(int x, int y)
          Set the location of the popup component
 void setModel(javax.swing.ButtonModel newModel)
          Set the data model for the "menu button" -- the label that the user clicks to open or close the menu.
 void setPopupMenuVisible(boolean b)
          Set the visibility of the Menu's popup portion.
 void setSelected(boolean b)
          Sets the selection status of the menu.
 void updateUI()
          Notification from the UIFactory that the L&F has changed.
 
Methods inherited from class javax.swing.JMenuItem
addMenuDragMouseListener, addMenuKeyListener, getAccelerator, isArmed, processKeyEvent, processMenuDragMouseEvent, processMenuKeyEvent, processMouseEvent, removeMenuDragMouseListener, removeMenuKeyListener, setArmed, setEnabled, setUI
 
Methods inherited from class javax.swing.AbstractButton
addActionListener, addChangeListener, addItemListener, doClick, getActionCommand, getDisabledIcon, getDisabledSelectedIcon, getHorizontalAlignment, getHorizontalTextPosition, getIcon, getLabel, getMargin, getMnemonic, getModel, getPressedIcon, getRolloverIcon, getRolloverSelectedIcon, getSelectedIcon, getSelectedObjects, getText, getUI, getVerticalAlignment, getVerticalTextPosition, isBorderPainted, isContentAreaFilled, isFocusPainted, isRolloverEnabled, removeActionListener, removeChangeListener, removeItemListener, setActionCommand, setBorderPainted, setContentAreaFilled, setDisabledIcon, setDisabledSelectedIcon, setFocusPainted, setHorizontalAlignment, setHorizontalTextPosition, setIcon, setLabel, setMargin, setMnemonic, setMnemonic, setPressedIcon, setRolloverEnabled, setRolloverIcon, setRolloverSelectedIcon, setSelectedIcon, setText, setUI, setVerticalAlignment, setVerticalTextPosition
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getActionForKeyStroke, getAlignmentX, getAlignmentY, getAutoscrolls, getBorder, getBounds, getClientProperty, getConditionForKeyStroke, getDebugGraphicsOptions, getGraphics, getHeight, getInsets, getInsets, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getVisibleRect, getWidth, getX, getY, grabFocus, hasFocus, isDoubleBuffered, isFocusCycleRoot, isFocusTraversable, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintImmediately, paintImmediately, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDoubleBuffered, setFont, setForeground, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, addContainerListener, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getLayout, insets, invalidate, isAncestorOf, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, removeContainerListener, setLayout, validate
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, bounds, checkImage, checkImage, contains, createImage, createImage, disable, dispatchEvent, enable, enable, enableInputMethods, getBackground, getBounds, getColorModel, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocationOnScreen, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hide, imageUpdate, inside, isDisplayable, isEnabled, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, remove, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JMenu

public JMenu()
Creates a new JMenu with no text.

JMenu

public JMenu(java.lang.String s)
Creates a new JMenu with the supplied string as its text
Parameters:
s - The text for the menu label

JMenu

public JMenu(java.lang.String s,
             boolean b)
Creates a new JMenu with the supplied string as its text and specified as a tear-off menu or not.
Parameters:
s - The text for the menu label
b - can the menu be torn off (not yet implemented)
Method Detail

updateUI

public void updateUI()
Notification from the UIFactory that the L&F has changed. Called to replace the UI with the latest version from the UIFactory.
Overrides:
updateUI in class javax.swing.JMenuItem
See Also:
JComponent.updateUI()

getUIClassID

public java.lang.String getUIClassID()
Returns the name of the L&F class that renders this component.
Returns:
"MenuUI"
Overrides:
getUIClassID in class javax.swing.JMenuItem
See Also:
JComponent.getUIClassID(), UIDefaults#getUI

setModel

public void setModel(javax.swing.ButtonModel newModel)
Set the data model for the "menu button" -- the label that the user clicks to open or close the menu.
Parameters:
m - the ButtonModel
Overrides:
setModel in class javax.swing.AbstractButton
See Also:
AbstractButton.getModel()

isSelected

public boolean isSelected()
Returns true if the menu is currently selected (popped up).
Returns:
true if the menu is open, else false
Overrides:
isSelected in class javax.swing.AbstractButton

setSelected

public void setSelected(boolean b)
Sets the selection status of the menu.
Parameters:
b - a boolean value -- true to select the menu and open it, false to unselect the menu and close it
Overrides:
setSelected in class javax.swing.AbstractButton

isPopupMenuVisible

public boolean isPopupMenuVisible()
Returns true if the menu's popup window is visible.
Returns:
true if the menu is visible, else false

setPopupMenuVisible

public void setPopupMenuVisible(boolean b)
Set the visibility of the Menu's popup portion. The popup may only be made visible if the menu is itself showing on the screen.
Parameters:
b - a boolean value -- true to make the menu visible, false to hide it

getDelay

public int getDelay()
Returns the suggested delay before the menu's PopupMenu is popped up or down.
Returns:
an int -- the number of milliseconds to delay

setDelay

public void setDelay(int d)
Sets the suggested delay before the menu's PopupMenu is popped up or down.
Parameters:
d - the number of milliseconds to delay
Throws:
java.lang.IllegalArgumentException - if the value of d is less than 0.

setMenuLocation

public void setMenuLocation(int x,
                            int y)
Set the location of the popup component
Parameters:
x - the x coordinate of the popup's new position
y - the y coordinate of the popup's new position

add

public javax.swing.JMenuItem add(javax.swing.JMenuItem menuItem)
Appends a menuitem to the end of this menu. Returns the menuitem added.
Parameters:
menuItem - the JMenuitem to be added
Returns:
the JMenuItem added

add

public java.awt.Component add(java.awt.Component c)
Appends a component to the end of this menu. Returns the component added.
Parameters:
c - the Component to add
Returns:
the Component added
Overrides:
add in class java.awt.Container

add

public javax.swing.JMenuItem add(java.lang.String s)
Creates a new menuitem with the specified text and appends it to the end of this menu.
Parameters:
s - the string for the menuitem to be added

add

public javax.swing.JMenuItem add(javax.swing.Action a)
Creates a new menuitem attached to the specified Action object and appends it to the end of this menu.
Parameters:
a - the Action for the menuitem to be added
See Also:
Action

addSeparator

public void addSeparator()
Append a new separator to the end of the menu.

insert

public void insert(java.lang.String s,
                   int pos)
Insert a new menuitem with the specified text at a given position.
Parameters:
s - the text for the menuitem to add
pos - an int giving the position at which to add the new menuitem

insert

public javax.swing.JMenuItem insert(javax.swing.JMenuItem mi,
                                    int pos)
Insert the specified JMenuitem at a given position.
Parameters:
mi - the JMenuitem to add
pos - an int giving the position at which to add the new JMenuitem

insert

public javax.swing.JMenuItem insert(javax.swing.Action a,
                                    int pos)
Insert a new menuitem attached to the specified Action object at a given position.
Parameters:
a - the Action object for the menuitem to add
pos - an int giving the position at which to add the new menuitem

insertSeparator

public void insertSeparator(int index)
Inserts a separator at the specified position.
Parameters:
index - an int giving the position at which to insert the menu separator
Throws:
java.lang.IllegalArgumentException - if the value of index is less than 0.

getItem

public javax.swing.JMenuItem getItem(int pos)
Returns the JMenuItem at the specified position. If the specified position contains a separator, this JMenu is returned.
Parameters:
pos - an int giving the position
Throws:
java.lang.IllegalArgumentException - if the value of index is less than 0.

getItemCount

public int getItemCount()
Returns the number of items on the menu, including separators. This method is included for AWT compatibility.
Returns:
an int equal to the number of items on the menu
See Also:
getMenuComponentCount()

isTearOff

public boolean isTearOff()
Returns true if the menu can be torn off.
Returns:
true if the menu can be torn off, else false

remove

public void remove(javax.swing.JMenuItem item)
Removes the specified menu item from this menu.
Parameters:
item - the JMenuItem to be removed from the menu

remove

public void remove(int pos)
Removes the menu item at the specified index from this menu.
Parameters:
index - the position of the item to be removed.
Throws:
java.lang.IllegalArgumentException - if the value of index is less than 0.
Overrides:
remove in class java.awt.Container

remove

public void remove(java.awt.Component c)
Removes the Component from this menu.
Parameters:
c - the component to be removed
Overrides:
remove in class java.awt.Container

removeAll

public void removeAll()
Remove all menu items from this menu.
Overrides:
removeAll in class java.awt.Container

getMenuComponentCount

public int getMenuComponentCount()
Returns the number of components on the menu.
Returns:
an int -- the number of components on the menu

getMenuComponent

public java.awt.Component getMenuComponent(int n)
Returns the component at position n
Parameters:
n - the position of the component to be returned

getMenuComponents

public java.awt.Component[] getMenuComponents()
Returns an array of the menu's subcomponents
Returns:
an array of Components

isTopLevelMenu

public boolean isTopLevelMenu()
Returns true if the menu is a 'top-level menu', that is, if it is the direct child of a menubar.
Returns:
true if the menu is activated from the menu bar, false if the menu is activated from a menu item on another menu

isMenuComponent

public boolean isMenuComponent(java.awt.Component c)
Returns true if the specified component exists in the submenu hierarchy.
Parameters:
c - the Component to be tested
Returns:
true if the component exists

getPopupMenu

public javax.swing.JPopupMenu getPopupMenu()
Returns the popupmenu associated with this menu

addMenuListener

public void addMenuListener(javax.swing.event.MenuListener l)
Add a listener for menu events
Parameters:
l - the listener to be added

removeMenuListener

public void removeMenuListener(javax.swing.event.MenuListener l)
Remove a listener for menu events
Parameters:
l - the listener to be removed

menuSelectionChanged

public void menuSelectionChanged(boolean isIncluded)
Messaged when the menubar selection changes to activate or deactivate this menu. Overrides JMenuItem.menuSelectionChanged.
Specified by:
menuSelectionChanged in interface javax.swing.MenuElement
Parameters:
isIncluded - true if this menu is active, false if it is not
Overrides:
menuSelectionChanged in class javax.swing.JMenuItem

getSubElements

public javax.swing.MenuElement[] getSubElements()
Returns an array containing the sub-menu components for this menu component
Specified by:
getSubElements in interface javax.swing.MenuElement
Returns:
an array of MenuElement objects
Overrides:
getSubElements in class javax.swing.JMenuItem

getComponent

public java.awt.Component getComponent()
This method returns the java.awt.Component used to paint this MenuElement. The returned component is used to convert events and detect if an event is inside a menu component.
Specified by:
getComponent in interface javax.swing.MenuElement
Overrides:
getComponent in class javax.swing.JMenuItem

setAccelerator

public void setAccelerator(javax.swing.KeyStroke keyStroke)
setAccelerator() is not defined for JMenu. Use setMnemonic() instead.
Overrides:
setAccelerator in class javax.swing.JMenuItem

doClick

public void doClick(int pressTime)
Programatically perform a "click". This overrides the method AbstractButton.doClick(int) in order to make the menu pop up.
Overrides:
doClick in class javax.swing.AbstractButton

getAccessibleContext

public javax.accessibility.AccessibleContext getAccessibleContext()
Get the AccessibleContext associated with this JComponent
Specified by:
getAccessibleContext in interface javax.accessibility.Accessible
Returns:
the AccessibleContext of this JComponent
Overrides:
getAccessibleContext in class javax.swing.JMenuItem