Package Summary  Overview Summary

class:JMenu [CHANGED]

All Implemented Interfaces:
ImageObserver, ItemSelectable, MenuContainer, Serializable, Accessible, MenuElement, SwingConstants

@JavaBean(description="A popup window containing menu items displayed in a menu bar.")
public class JMenu
extends JMenuItem
implements Accessible, 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.

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™JavaBeans has been added to the java.beans package. Please see XMLEncoder.

Since:
1.2
See Also:
JMenuItem, JSeparator, JMenuBar, JPopupMenu
All Implemented Interfaces:
ImageObserver, ItemSelectable, MenuContainer, Serializable, Accessible, MenuElement, SwingConstants

@JavaBean(description="A popup window containing menu items displayed in a menu bar.")
public class JMenu
extends JMenuItem
implements Accessible, 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.

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 XMLEncoder.

Since:
1.2
See Also:
JMenuItem, JSeparator, JMenuBar, JPopupMenu
All Implemented Interfaces:
ImageObserver, ItemSelectable, MenuContainer, Serializable, Accessible, MenuElement, SwingConstants

@JavaBean(description="A popup window containing menu items displayed in a menu bar.")
public class JMenu
extends JMenuItem
implements Accessible, 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.

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 XMLEncoder.

Since:
1.2
See Also:
JMenuItem, JSeparator, JMenuBar, JPopupMenu

field:popupListener [NONE]

constructor:JMenu() [NONE]

  • JMenu

    public JMenu()
    Constructs a new JMenu with no text.
  • constructor:JMenu(java.lang.String) [NONE]

    JMenu

    public JMenu?(String s)
    Constructs a new JMenu with the supplied string as its text.
    Parameters:
    s - the text for the menu label

    constructor:JMenu(javax.swing.Action) [NONE]

    JMenu

    public JMenu?(Action a)
    Constructs a menu whose properties are taken from the Action supplied.
    Parameters:
    a - an Action
    Since:
    1.3

    constructor:JMenu(java.lang.String,boolean) [NONE]

    JMenu

    public JMenu?(String s, boolean b)
    Constructs 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:updateUI() [NONE]

  • updateUI

    public void updateUI()
    Resets the UI property with a value from the current look and feel.
    Overrides:
    updateUI in class JMenuItem
    See Also:
    JComponent.updateUI()
  • method:getUIClassID() [NONE]

    getUIClassID

    @BeanProperty(bound=false) public String getUIClassID()
    Returns the name of the L&F class that renders this component.
    Overrides:
    getUIClassID in class JMenuItem
    Returns:
    the string "MenuUI"
    See Also:
    JComponent.getUIClassID(), UIDefaults.getUI(javax.swing.JComponent)

    method:setModel(javax.swing.ButtonModel) [NONE]

    setModel

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

    method:isSelected() [NONE]

    isSelected

    public boolean isSelected()
    Returns true if the menu is currently selected (highlighted).
    Overrides:
    isSelected in class AbstractButton
    Returns:
    true if the menu is selected, else false

    method:setSelected(boolean) [NONE]

    setSelected

    @BeanProperty(expert=true, hidden=true, description="When the menu is selected, its popup child is shown.") public void setSelected?(boolean b)
    Sets the selection status of the menu.
    Overrides:
    setSelected in class AbstractButton
    Parameters:
    b - true to select (highlight) the menu; false to de-select the menu

    method:isPopupMenuVisible() [NONE]

    isPopupMenuVisible

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

    method:setPopupMenuVisible(boolean) [NONE]

    setPopupMenuVisible

    @BeanProperty(bound=false, expert=true, hidden=true, description="The popup menu\'s visibility") public void setPopupMenuVisible?(boolean b)
    Sets the visibility of the menu's popup. If the menu is not enabled, this method will have no effect.
    Parameters:
    b - a boolean value -- true to make the menu visible, false to hide it

    method:getPopupMenuOrigin() [NONE]

    getPopupMenuOrigin

    protected Point getPopupMenuOrigin()
    Computes the origin for the JMenu's popup menu. This method uses Look and Feel properties named Menu.menuPopupOffsetX, Menu.menuPopupOffsetY, Menu.submenuPopupOffsetX, and Menu.submenuPopupOffsetY to adjust the exact location of popup.
    Returns:
    a Point in the coordinate space of the menu which should be used as the origin of the JMenu's popup menu
    Since:
    1.3

    method:getDelay() [NONE]

    getDelay

    public int getDelay()
    Returns the suggested delay, in milliseconds, before submenus are popped up or down. Each look and feel (L&F) may determine its own policy for observing the delay property. In most cases, the delay is not observed for top level menus or while dragging. The default for delay is 0. This method is a property of the look and feel code and is used to manage the idiosyncrasies of the various UI implementations.
    Returns:
    the delay property

    method:setDelay(int) [NONE]

    setDelay

    @BeanProperty(bound=false, expert=true, description="The delay between menu selection and making the popup menu visible") public void setDelay?(int d)
    Sets the suggested delay before the menu's PopupMenu is popped up or down. Each look and feel (L&F) may determine it's own policy for observing the delay property. In most cases, the delay is not observed for top level menus or while dragging. This method is a property of the look and feel code and is used to manage the idiosyncrasies of the various UI implementations.
    Parameters:
    d - the number of milliseconds to delay
    Throws:
    IllegalArgumentException - if d is less than 0

    method:setMenuLocation(int,int) [NONE]

    setMenuLocation

    public void setMenuLocation?(int x, int y)
    Sets 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

    method:add(javax.swing.JMenuItem) [NONE]

    add

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

    method:add(java.awt.Component) [NONE]

    add

    public Component add?(Component c)
    Appends a component to the end of this menu. Returns the component added.
    Overrides:
    add in class Container
    Parameters:
    c - the Component to add
    Returns:
    the Component added
    See Also:
    Container.addImpl(java.awt.Component, java.lang.Object, int) , Container.invalidate(), Container.validate(), JComponent.revalidate()

    method:add(java.awt.Component,int) [NONE]

    add

    public Component add?(Component c, int index)
    Adds the specified component to this container at the given position. If index equals -1, the component will be appended to the end.
    Overrides:
    add in class Container
    Parameters:
    c - the Component to add
    index - the position at which to insert the component
    Returns:
    the Component added
    See Also:
    remove(javax.swing.JMenuItem), Container.add(Component, int)

    method:add(java.lang.String) [NONE]

    add

    public JMenuItem add?(String s)
    Creates a new menu item with the specified text and appends it to the end of this menu.
    Parameters:
    s - the string for the menu item to be added
    Returns:
    the new JMenuItem

    method:add(javax.swing.Action) [NONE]

    add

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

    method:createActionComponent(javax.swing.Action) [NONE]

    createActionComponent

    protected JMenuItem createActionComponent?(Action a)
    Factory method which creates the JMenuItem for Actions added to the JMenu.
    Parameters:
    a - the Action for the menu item to be added
    Returns:
    the new menu item
    Since:
    1.3
    See Also:
    Action

    method:createActionChangeListener(javax.swing.JMenuItem) [NONE]

    createActionChangeListener

    protected PropertyChangeListener createActionChangeListener?(JMenuItem b)
    Returns a properly configured PropertyChangeListener which updates the control as changes to the Action occur.
    Parameters:
    b - a menu item for which to create a PropertyChangeListener
    Returns:
    a PropertyChangeListener for b

    method:addSeparator() [NONE]

    addSeparator

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

    method:insert(java.lang.String,int) [NONE]

    insert

    public void insert?(String s, int pos)
    Inserts a new menu item with the specified text at a given position.
    Parameters:
    s - the text for the menu item to add
    pos - an integer specifying the position at which to add the new menu item
    Throws:
    IllegalArgumentException - when the value of pos < 0

    method:insert(javax.swing.JMenuItem,int) [NONE]

    insert

    public JMenuItem insert?(JMenuItem mi, int pos)
    Inserts the specified JMenuitem at a given position.
    Parameters:
    mi - the JMenuitem to add
    pos - an integer specifying the position at which to add the new JMenuitem
    Returns:
    the new menu item
    Throws:
    IllegalArgumentException - if the value of pos < 0

    method:insert(javax.swing.Action,int) [NONE]

    insert

    public JMenuItem insert?(Action a, int pos)
    Inserts a new menu item attached to the specified Action object at a given position.
    Parameters:
    a - the Action object for the menu item to add
    pos - an integer specifying the position at which to add the new menu item
    Returns:
    the new menu item
    Throws:
    IllegalArgumentException - if the value of pos < 0

    method:insertSeparator(int) [NONE]

    insertSeparator

    public void insertSeparator?(int index)
    Inserts a separator at the specified position.
    Parameters:
    index - an integer specifying the position at which to insert the menu separator
    Throws:
    IllegalArgumentException - if the value of index < 0

    method:getItem(int) [NONE]

    getItem

    public JMenuItem getItem?(int pos)
    Returns the JMenuItem at the specified position. If the component at pos is not a menu item, null is returned. This method is included for AWT compatibility.
    Parameters:
    pos - an integer specifying the position
    Returns:
    the menu item at the specified position; or null if the item as the specified position is not a menu item
    Throws:
    IllegalArgumentException - if the value of pos < 0

    method:getItemCount() [NONE]

    getItemCount

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

    method:isTearOff() [NONE]

    isTearOff

    @BeanProperty(bound=false) public boolean isTearOff()
    Returns true if the menu can be torn off. This method is not yet implemented.
    Returns:
    true if the menu can be torn off, else false
    Throws:
    Error - if invoked -- this method is not yet implemented

    method:remove(javax.swing.JMenuItem) [NONE]

    remove

    public void remove?(JMenuItem item)
    Removes the specified menu item from this menu. If there is no popup menu, this method will have no effect.
    Parameters:
    item - the JMenuItem to be removed from the menu

    method:remove(int) [NONE]

    remove

    public void remove?(int pos)
    Removes the menu item at the specified index from this menu.
    Overrides:
    remove in class Container
    Parameters:
    pos - the position of the item to be removed
    Throws:
    IllegalArgumentException - if the value of pos < 0, or if pos is greater than the number of menu items
    See Also:
    Container.add(java.awt.Component), Container.invalidate(), Container.validate(), Container.getComponentCount()

    method:remove(java.awt.Component) [NONE]

    remove

    public void remove?(Component c)
    Removes the component c from this menu.
    Overrides:
    remove in class Container
    Parameters:
    c - the component to be removed
    See Also:
    Container.add(java.awt.Component), Container.invalidate(), Container.validate(), Container.remove(int)

    method:removeAll() [NONE]

    removeAll

    public void removeAll()
    Removes all menu items from this menu.
    Overrides:
    removeAll in class Container
    See Also:
    Container.add(java.awt.Component), Container.remove(int), Container.invalidate()

    method:getMenuComponentCount() [NONE]

    getMenuComponentCount

    @BeanProperty(bound=false) public int getMenuComponentCount()
    Returns the number of components on the menu.
    Returns:
    an integer containing the number of components on the menu

    method:getMenuComponent(int) [NONE]

    getMenuComponent

    public Component getMenuComponent?(int n)
    Returns the component at position n.
    Parameters:
    n - the position of the component to be returned
    Returns:
    the component requested, or null if there is no popup menu

    method:getMenuComponents() [NONE]

    getMenuComponents

    @BeanProperty(bound=false) public Component[] getMenuComponents()
    Returns an array of Components of the menu's subcomponents. Note that this returns all Components in the popup menu, including separators.
    Returns:
    an array of Components or an empty array if there is no popup menu

    method:isTopLevelMenu() [NONE]

    isTopLevelMenu

    @BeanProperty(bound=false) 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

    method:isMenuComponent(java.awt.Component) [NONE]

    isMenuComponent

    public boolean isMenuComponent?(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, false otherwise

    method:getPopupMenu() [NONE]

    getPopupMenu

    @BeanProperty(bound=false) public JPopupMenu getPopupMenu()
    Returns the popupmenu associated with this menu. If there is no popupmenu, it will create one.
    Returns:
    the JPopupMenu associated with this menu

    method:addMenuListener(javax.swing.event.MenuListener) [NONE]

    addMenuListener

    public void addMenuListener?(MenuListener l)
    Adds a listener for menu events.
    Parameters:
    l - the listener to be added

    method:removeMenuListener(javax.swing.event.MenuListener) [NONE]

    removeMenuListener

    public void removeMenuListener?(MenuListener l)
    Removes a listener for menu events.
    Parameters:
    l - the listener to be removed

    method:getMenuListeners() [NONE]

    getMenuListeners

    @BeanProperty(bound=false) public MenuListener[] getMenuListeners()
    Returns an array of all the MenuListeners added to this JMenu with addMenuListener().
    Returns:
    all of the MenuListeners added or an empty array if no listeners have been added
    Since:
    1.4

    method:fireMenuSelected() [NONE]

    fireMenuSelected

    protected void fireMenuSelected()
    Notifies all listeners that have registered interest for notification on this event type. The event instance is created lazily.
    Throws:
    Error - if there is a null listener
    See Also:
    EventListenerList

    method:fireMenuDeselected() [NONE]

    fireMenuDeselected

    protected void fireMenuDeselected()
    Notifies all listeners that have registered interest for notification on this event type. The event instance is created lazily.
    Throws:
    Error - if there is a null listener
    See Also:
    EventListenerList

    method:fireMenuCanceled() [NONE]

    fireMenuCanceled

    protected void fireMenuCanceled()
    Notifies all listeners that have registered interest for notification on this event type. The event instance is created lazily.
    Throws:
    Error - if there is a null listener
    See Also:
    EventListenerList

    method:createWinListener(javax.swing.JPopupMenu) [NONE]

    createWinListener

    protected JMenu.WinListener createWinListener?(JPopupMenu p)
    Creates a window-closing listener for the popup.
    Parameters:
    p - the JPopupMenu
    Returns:
    the new window-closing listener
    See Also:
    JMenu.WinListener

    method:menuSelectionChanged(boolean) [NONE]

    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 MenuElement
    Overrides:
    menuSelectionChanged in class JMenuItem
    Parameters:
    isIncluded - true if this menu is active, false if it is not
    See Also:
    MenuSelectionManager.setSelectedPath(MenuElement[])

    method:getSubElements() [NONE]

    getSubElements

    @BeanProperty(bound=false) public MenuElement[] getSubElements()
    Returns an array of MenuElements containing the submenu for this menu component. If popup menu is null returns an empty array. This method is required to conform to the MenuElement interface. Note that since JSeparators do not conform to the MenuElement interface, this array will only contain JMenuItems.
    Specified by:
    getSubElements in interface MenuElement
    Overrides:
    getSubElements in class JMenuItem
    Returns:
    an array of MenuElement objects

    method:getComponent() [NONE]

    getComponent

    public Component getComponent()
    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 MenuElement
    Overrides:
    getComponent in class JMenuItem
    Returns:
    the Component that paints this menu item

    method:applyComponentOrientation(java.awt.ComponentOrientation) [NONE]

    applyComponentOrientation

    public void applyComponentOrientation?(ComponentOrientation o)
    Sets the ComponentOrientation property of this menu and all components contained within it. This includes all components returned by getMenuComponents.
    Overrides:
    applyComponentOrientation in class Container
    Parameters:
    o - the new component orientation of this menu and the components contained within it.
    Throws:
    NullPointerException - if orientation is null.
    Since:
    1.4
    See Also:
    Component.setComponentOrientation(java.awt.ComponentOrientation), Component.getComponentOrientation()

    method:setComponentOrientation(java.awt.ComponentOrientation) [NONE]

    setComponentOrientation

    public void setComponentOrientation?(ComponentOrientation o)
    Description copied from class: Component
    Sets the language-sensitive orientation that is to be used to order the elements or text within this component. Language-sensitive LayoutManager and Component subclasses will use this property to determine how to lay out and draw components.

    At construction time, a component's orientation is set to ComponentOrientation.UNKNOWN, indicating that it has not been specified explicitly. The UNKNOWN orientation behaves the same as ComponentOrientation.LEFT_TO_RIGHT.

    To set the orientation of a single component, use this method. To set the orientation of an entire component hierarchy, use applyComponentOrientation.

    This method changes layout-related information, and therefore, invalidates the component hierarchy.

    Overrides:
    setComponentOrientation in class Component
    Parameters:
    o - the orientation to be set
    See Also:
    ComponentOrientation, Component.invalidate()

    method:setAccelerator(javax.swing.KeyStroke) [NONE]

    setAccelerator

    public void setAccelerator?(KeyStroke keyStroke)
    setAccelerator is not defined for JMenu. Use setMnemonic instead.
    Overrides:
    setAccelerator in class JMenuItem
    Parameters:
    keyStroke - the keystroke combination which will invoke the JMenuItem's actionlisteners without navigating the menu hierarchy
    Throws:
    Error - if invoked -- this method is not defined for JMenu. Use setMnemonic instead

    method:processKeyEvent(java.awt.event.KeyEvent) [NONE]

    processKeyEvent

    protected void processKeyEvent?(KeyEvent evt)
    Processes key stroke events such as mnemonics and accelerators.
    Overrides:
    processKeyEvent in class JComponent
    Parameters:
    evt - the key event to be processed
    See Also:
    KeyEvent, KeyListener, KeyboardFocusManager, DefaultKeyboardFocusManager, Component.processEvent(java.awt.AWTEvent), Component.dispatchEvent(java.awt.AWTEvent), Component.addKeyListener(java.awt.event.KeyListener), Component.enableEvents(long), Component.isShowing()

    method:doClick(int) [NONE]

    doClick

    public void doClick?(int pressTime)
    Programmatically performs a "click". This overrides the method AbstractButton.doClick in order to make the menu pop up.
    Overrides:
    doClick in class AbstractButton
    Parameters:
    pressTime - indicates the number of milliseconds the button was pressed for

    method:paramString() [NONE]

    paramString

    protected String paramString()
    Returns a string representation of this JMenu. 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 be null.
    Overrides:
    paramString in class JMenuItem
    Returns:
    a string representation of this JMenu.

    method:getAccessibleContext() [NONE]

    getAccessibleContext

    @BeanProperty(bound=false) public AccessibleContext getAccessibleContext()
    Gets the AccessibleContext associated with this JMenu. For JMenus, the AccessibleContext takes the form of an AccessibleJMenu. A new AccessibleJMenu instance is created if necessary.
    Specified by:
    getAccessibleContext in interface Accessible
    Overrides:
    getAccessibleContext in class JMenuItem
    Returns:
    an AccessibleJMenu that serves as the AccessibleContext of this JMenu

    © 2020 Oracle Corporation and/or its affiliates