JMenu (Java 2 Platform SE 5.0) (original) (raw)
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
All Implemented Interfaces:
ImageObserver, ItemSelectable, MenuContainer, Serializable, Accessible, MenuElement, SwingConstants
public class JMenu
extends JMenuItem
implements Accessible, MenuElement
An implementation of a menu -- a popup window containingJMenuItem
s that is displayed when the user selects an item on the JMenuBar
. In addition to JMenuItem
s, a JMenu
can also contain JSeparator
s.
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 information and examples of using menus seeHow to Use Menus, a section in The Java Tutorial.
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 JavaBeansTM has been added to the java.beans
package. Please see XMLEncoder.
See Also:
JMenuItem, JSeparator, JMenuBar, JPopupMenu
Nested Class Summary | |
---|---|
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. |
Nested classes/interfaces inherited from class javax.swing.JMenuItem |
---|
JMenuItem.AccessibleJMenuItem |
Nested classes/interfaces inherited from class javax.swing.AbstractButton |
---|
AbstractButton.AccessibleAbstractButton, AbstractButton.ButtonChangeListener |
Nested classes/interfaces inherited from class javax.swing.JComponent |
---|
JComponent.AccessibleJComponent |
Nested classes/interfaces inherited from class java.awt.Container |
---|
Container.AccessibleAWTContainer |
Nested classes/interfaces inherited from class java.awt.Component |
---|
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy |
Field Summary | |
---|---|
protected JMenu.WinListener | popupListener The window-closing listener for the popup. |
Fields inherited from class javax.swing.JComponent |
---|
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, 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 |
Fields inherited from interface javax.swing.SwingConstants |
---|
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST |
Fields inherited from interface java.awt.image.ImageObserver |
---|
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
Constructor Summary |
---|
JMenu() Constructs a new JMenu with no text. |
JMenu(Action a) Constructs a menu whose properties are taken from the Action supplied. |
JMenu(String s) Constructs a new JMenu with the supplied string as its text. |
[JMenu](../../javax/swing/JMenu.html#JMenu%28java.lang.String, boolean%29)(String s, boolean b) Constructs a new JMenu with the supplied string as its text and specified as a tear-off menu or not. |
Method Summary | |
---|---|
JMenuItem | add(Action a) Creates a new menu item attached to the specified Action object and appends it to the end of this menu. |
Component | add(Component c) Appends a component to the end of this menu. |
Component | [add](../../javax/swing/JMenu.html#add%28java.awt.Component, int%29)(Component c, int index) Adds the specified component to this container at the given position. |
JMenuItem | add(JMenuItem menuItem) Appends a menu item to the end of this menu. |
JMenuItem | add(String s) Creates a new menu item with the specified text and appends it to the end of this menu. |
void | addMenuListener(MenuListener l) Adds a listener for menu events. |
void | addSeparator() Appends a new separator to the end of the menu. |
void | applyComponentOrientation(ComponentOrientation o) Sets the ComponentOrientation property of this menu and all components contained within it. |
protected void | configurePropertiesFromAction(Action a) Factory method which sets the ActionEvent source's properties according to values from theAction instance. |
protected PropertyChangeListener | createActionChangeListener(JMenuItem b) Returns a properly configured PropertyChangeListener which updates the control as changes to the Action occur. |
protected JMenuItem | createActionComponent(Action a) Factory method which creates the JMenuItem for Actions added to the JMenu. |
protected JMenu.WinListener | createWinListener(JPopupMenu p) Creates a window-closing listener for the popup. |
void | doClick(int pressTime) Programmatically performs a "click". |
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. |
AccessibleContext | getAccessibleContext() Gets the AccessibleContext associated with this JMenu. |
Component | getComponent() Returns the java.awt.Component used to paint this MenuElement. |
int | getDelay() Returns the suggested delay, in milliseconds, before submenus are popped up or down. |
JMenuItem | getItem(int pos) Returns the JMenuItem at the specified position. |
int | getItemCount() Returns the number of items on the menu, including separators. |
Component | getMenuComponent(int n) Returns the component at position n. |
int | getMenuComponentCount() Returns the number of components on the menu. |
Component[] | getMenuComponents() Returns an array of Components of the menu's subcomponents. |
MenuListener[] | getMenuListeners() Returns an array of all the MenuListeners added to this JMenu with addMenuListener(). |
JPopupMenu | getPopupMenu() Returns the popupmenu associated with this menu. |
protected Point | getPopupMenuOrigin() Computes the origin for the JMenu's popup menu. |
MenuElement[] | getSubElements() Returns an array of MenuElements containing the submenu for this menu component. |
String | getUIClassID() Returns the name of the L&F class that renders this component. |
JMenuItem | [insert](../../javax/swing/JMenu.html#insert%28javax.swing.Action, int%29)(Action a, int pos) Inserts a new menu item attached to the specified Action object at a given position. |
JMenuItem | [insert](../../javax/swing/JMenu.html#insert%28javax.swing.JMenuItem, int%29)(JMenuItem mi, int pos) Inserts the specified JMenuitem at a given position. |
void | [insert](../../javax/swing/JMenu.html#insert%28java.lang.String, int%29)(String s, int pos) Inserts a new menu item with the specified text at a given position. |
void | insertSeparator(int index) Inserts a separator at the specified position. |
boolean | isMenuComponent(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 (highlighted). |
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. |
protected String | paramString() Returns a string representation of this JMenu. |
protected void | processKeyEvent(KeyEvent evt) Processes key stroke events such as mnemonics and accelerators. |
void | remove(Component c) Removes the component c from this menu. |
void | remove(int pos) Removes the menu item at the specified index from this menu. |
void | remove(JMenuItem item) Removes the specified menu item from this menu. |
void | removeAll() Removes all menu items from this menu. |
void | removeMenuListener(MenuListener l) Removes a listener for menu events. |
void | setAccelerator(KeyStroke keyStroke) setAccelerator is not defined for JMenu. |
void | setComponentOrientation(ComponentOrientation o) Sets the language-sensitive orientation that is to be used to order the elements or text within this component. |
void | setDelay(int d) Sets the suggested delay before the menu's PopupMenu is popped up or down. |
void | [setMenuLocation](../../javax/swing/JMenu.html#setMenuLocation%28int, int%29)(int x, int y) Sets the location of the popup component. |
void | setModel(ButtonModel newModel) Sets the data model for the "menu button" -- the label that the user clicks to open or close the menu. |
void | setPopupMenuVisible(boolean b) Sets the visibility of the menu's popup. |
void | setSelected(boolean b) Sets the selection status of the menu. |
void | updateUI() Resets the UI property with a value from the current look and feel. |
Methods inherited from class javax.swing.JMenuItem |
---|
addMenuDragMouseListener, addMenuKeyListener, createActionPropertyChangeListener, fireMenuDragMouseDragged, fireMenuDragMouseEntered, fireMenuDragMouseExited, fireMenuDragMouseReleased, fireMenuKeyPressed, fireMenuKeyReleased, fireMenuKeyTyped, getAccelerator, getMenuDragMouseListeners, getMenuKeyListeners, [init](../../javax/swing/JMenuItem.html#init%28java.lang.String, javax.swing.Icon%29), isArmed, [processKeyEvent](../../javax/swing/JMenuItem.html#processKeyEvent%28java.awt.event.KeyEvent, javax.swing.MenuElement[], javax.swing.MenuSelectionManager%29), processMenuDragMouseEvent, processMenuKeyEvent, [processMouseEvent](../../javax/swing/JMenuItem.html#processMouseEvent%28java.awt.event.MouseEvent, javax.swing.MenuElement[], javax.swing.MenuSelectionManager%29), removeMenuDragMouseListener, removeMenuKeyListener, setArmed, setEnabled, setUI |
Methods inherited from class javax.swing.JComponent |
---|
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, [contains](../../javax/swing/JComponent.html#contains%28int, int%29), createToolTip, disable, enable, [firePropertyChange](../../javax/swing/JComponent.html#firePropertyChange%28java.lang.String, boolean, boolean%29), [firePropertyChange](../../javax/swing/JComponent.html#firePropertyChange%28java.lang.String, char, char%29), [firePropertyChange](../../javax/swing/JComponent.html#firePropertyChange%28java.lang.String, int, int%29), [fireVetoableChange](../../javax/swing/JComponent.html#fireVetoableChange%28java.lang.String, java.lang.Object, java.lang.Object%29), getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintChildren, paintComponent, [paintImmediately](../../javax/swing/JComponent.html#paintImmediately%28int, int, int, int%29), paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, [processKeyBinding](../../javax/swing/JComponent.html#processKeyBinding%28javax.swing.KeyStroke, java.awt.event.KeyEvent, int, boolean%29), processMouseEvent, processMouseMotionEvent, [putClientProperty](../../javax/swing/JComponent.html#putClientProperty%28java.lang.Object, java.lang.Object%29), [registerKeyboardAction](../../javax/swing/JComponent.html#registerKeyboardAction%28java.awt.event.ActionListener, javax.swing.KeyStroke, int%29), [registerKeyboardAction](../../javax/swing/JComponent.html#registerKeyboardAction%28java.awt.event.ActionListener, java.lang.String, javax.swing.KeyStroke, int%29), removeAncestorListener, removeNotify, removeVetoableChangeListener, [repaint](../../javax/swing/JComponent.html#repaint%28long, int, int, int, int%29), repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, [reshape](../../javax/swing/JComponent.html#reshape%28int, int, int, int%29), revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, [setFocusTraversalKeys](../../javax/swing/JComponent.html#setFocusTraversalKeys%28int, java.util.Set%29), setFont, setForeground, setInheritsPopupMenu, [setInputMap](../../javax/swing/JComponent.html#setInputMap%28int, javax.swing.InputMap%29), setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update |
Methods inherited from class java.awt.Container |
---|
[add](../../java/awt/Container.html#add%28java.awt.Component, java.lang.Object%29), [add](../../java/awt/Container.html#add%28java.awt.Component, java.lang.Object, int%29), [add](../../java/awt/Container.html#add%28java.lang.String, java.awt.Component%29), addContainerListener, addPropertyChangeListener, [addPropertyChangeListener](../../java/awt/Container.html#addPropertyChangeListener%28java.lang.String, java.beans.PropertyChangeListener%29), areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, [findComponentAt](../../java/awt/Container.html#findComponentAt%28int, int%29), findComponentAt, getComponent, [getComponentAt](../../java/awt/Container.html#getComponentAt%28int, int%29), getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, [list](../../java/awt/Container.html#list%28java.io.PrintStream, int%29), [list](../../java/awt/Container.html#list%28java.io.PrintWriter, int%29), [locate](../../java/awt/Container.html#locate%28int, int%29), minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, removeContainerListener, [setComponentZOrder](../../java/awt/Container.html#setComponentZOrder%28java.awt.Component, int%29), setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, transferFocusBackward, transferFocusDownCycle, validate, validateTree |
Methods inherited from class java.awt.Component |
---|
[action](../../java/awt/Component.html#action%28java.awt.Event, java.lang.Object%29), add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, [checkImage](../../java/awt/Component.html#checkImage%28java.awt.Image, java.awt.image.ImageObserver%29), [checkImage](../../java/awt/Component.html#checkImage%28java.awt.Image, int, int, java.awt.image.ImageObserver%29), [coalesceEvents](../../java/awt/Component.html#coalesceEvents%28java.awt.AWTEvent, java.awt.AWTEvent%29), contains, createImage, [createImage](../../java/awt/Component.html#createImage%28int, int%29), [createVolatileImage](../../java/awt/Component.html#createVolatileImage%28int, int%29), [createVolatileImage](../../java/awt/Component.html#createVolatileImage%28int, int, java.awt.ImageCapabilities%29), disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, byte, byte%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, double, double%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, float, float%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, long, long%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, java.lang.Object, java.lang.Object%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, short, short%29), getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, [gotFocus](../../java/awt/Component.html#gotFocus%28java.awt.Event, java.lang.Object%29), handleEvent, hasFocus, hide, [inside](../../java/awt/Component.html#inside%28int, int%29), isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, [keyDown](../../java/awt/Component.html#keyDown%28java.awt.Event, int%29), [keyUp](../../java/awt/Component.html#keyUp%28java.awt.Event, int%29), list, list, list, location, [lostFocus](../../java/awt/Component.html#lostFocus%28java.awt.Event, java.lang.Object%29), [mouseDown](../../java/awt/Component.html#mouseDown%28java.awt.Event, int, int%29), [mouseDrag](../../java/awt/Component.html#mouseDrag%28java.awt.Event, int, int%29), [mouseEnter](../../java/awt/Component.html#mouseEnter%28java.awt.Event, int, int%29), [mouseExit](../../java/awt/Component.html#mouseExit%28java.awt.Event, int, int%29), [mouseMove](../../java/awt/Component.html#mouseMove%28java.awt.Event, int, int%29), [mouseUp](../../java/awt/Component.html#mouseUp%28java.awt.Event, int, int%29), [move](../../java/awt/Component.html#move%28int, int%29), nextFocus, paintAll, postEvent, [prepareImage](../../java/awt/Component.html#prepareImage%28java.awt.Image, java.awt.image.ImageObserver%29), [prepareImage](../../java/awt/Component.html#prepareImage%28java.awt.Image, int, int, java.awt.image.ImageObserver%29), processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, [removePropertyChangeListener](../../java/awt/Component.html#removePropertyChangeListener%28java.lang.String, java.beans.PropertyChangeListener%29), repaint, [repaint](../../java/awt/Component.html#repaint%28int, int, int, int%29), repaint, resize, [resize](../../java/awt/Component.html#resize%28int, int%29), [setBounds](../../java/awt/Component.html#setBounds%28int, int, int, int%29), setBounds, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, [setLocation](../../java/awt/Component.html#setLocation%28int, int%29), setLocation, setName, setSize, [setSize](../../java/awt/Component.html#setSize%28int, int%29), show, show, size, toString, transferFocus, transferFocusUpCycle |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, [wait](../../java/lang/Object.html#wait%28long, int%29) |
Methods inherited from interface javax.swing.MenuElement |
---|
[processKeyEvent](../../javax/swing/MenuElement.html#processKeyEvent%28java.awt.event.KeyEvent, javax.swing.MenuElement[], javax.swing.MenuSelectionManager%29), [processMouseEvent](../../javax/swing/MenuElement.html#processMouseEvent%28java.awt.event.MouseEvent, javax.swing.MenuElement[], javax.swing.MenuSelectionManager%29) |
Field Detail |
---|
popupListener
protected JMenu.WinListener popupListener
The window-closing listener for the popup.
See Also:
Constructor Detail |
---|
JMenu
public JMenu()
Constructs a new JMenu
with no text.
JMenu
public JMenu(String s)
Constructs a new JMenu
with the supplied string as its text.
Parameters:
s
- the text for the menu label
JMenu
public JMenu(Action a)
Constructs a menu whose properties are taken from the Action
supplied.
Parameters:
a
- an Action
Since:
1.3
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 Detail |
---|
updateUI
public void updateUI()
Resets the UI property with a value from the current look and feel.
Overrides:
[updateUI](../../javax/swing/JMenuItem.html#updateUI%28%29)
in class [JMenuItem](../../javax/swing/JMenuItem.html "class in javax.swing")
See Also:
getUIClassID
public String getUIClassID()
Returns the name of the L&F class that renders this component.
Overrides:
[getUIClassID](../../javax/swing/JMenuItem.html#getUIClassID%28%29)
in class [JMenuItem](../../javax/swing/JMenuItem.html "class in javax.swing")
Returns:
the string "MenuUI"
See Also:
JComponent.getUIClassID(), UIDefaults.getUI(javax.swing.JComponent)
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](../../javax/swing/AbstractButton.html#setModel%28javax.swing.ButtonModel%29)
in class [AbstractButton](../../javax/swing/AbstractButton.html "class in javax.swing")
Parameters:
newModel
- the ButtonModel
See Also:
isSelected
public boolean isSelected()
Returns true if the menu is currently selected (highlighted).
Overrides:
[isSelected](../../javax/swing/AbstractButton.html#isSelected%28%29)
in class [AbstractButton](../../javax/swing/AbstractButton.html "class in javax.swing")
Returns:
true if the menu is selected, else false
setSelected
public void setSelected(boolean b)
Sets the selection status of the menu.
Overrides:
[setSelected](../../javax/swing/AbstractButton.html#setSelected%28boolean%29)
in class [AbstractButton](../../javax/swing/AbstractButton.html "class in javax.swing")
Parameters:
b
- true to select (highlight) the menu; false to de-select the menu
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)
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
getPopupMenuOrigin
protected Point getPopupMenuOrigin()
Computes the origin for the JMenu
's popup menu. This method uses Look and Feel properties namedMenu.menuPopupOffsetX
,Menu.menuPopupOffsetY
,Menu.submenuPopupOffsetX
, andMenu.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
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 idiosyncracies of the various UI implementations.
Returns:
the delay
property
setDelay
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 idiosyncracies of the various UI implementations.
Parameters:
d
- the number of milliseconds to delay
Throws:
[IllegalArgumentException](../../java/lang/IllegalArgumentException.html "class in java.lang")
- if d
is less than 0
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
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
add
public Component add(Component c)
Appends a component to the end of this menu. Returns the component added.
Overrides:
[add](../../java/awt/Container.html#add%28java.awt.Component%29)
in class [Container](../../java/awt/Container.html "class in java.awt")
Parameters:
c
- the Component
to add
Returns:
the Component
added
See Also:
[Container.addImpl(java.awt.Component, java.lang.Object, int)](../../java/awt/Container.html#addImpl%28java.awt.Component, java.lang.Object, int%29), Container.validate(), JComponent.revalidate()
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](../../java/awt/Container.html#add%28java.awt.Component, int%29)
in class [Container](../../java/awt/Container.html "class in java.awt")
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)](../../java/awt/Container.html#add%28java.awt.Component, int%29)
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
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. As of 1.3, this is no longer the preferred method for adding Actions
to a container. Instead it is recommended to configure a control with an action using setAction
, and then add that control directly to the Container
.
Parameters:
a
- the Action
for the menu item to be added
See Also:
createActionComponent
protected JMenuItem createActionComponent(Action a)
Factory method which creates the JMenuItem
for Action
s added to the JMenu
. As of 1.3, this is no longer the preferred method. Instead it is recommended to configure a control with an action using setAction
, and then adding that control directly to the Container
.
Parameters:
a
- the Action
for the menu item to be added
Returns:
the new menu item
Since:
1.3
See Also:
createActionChangeListener
protected PropertyChangeListener createActionChangeListener(JMenuItem b)
Returns a properly configured PropertyChangeListener
which updates the control as changes to the Action
occur. As of 1.3, this is no longer the preferred method for addingAction
s to a Container
. Instead it is recommended to configure a control with an action using setAction
, and then add that control directly to the Container
.
addSeparator
public void addSeparator()
Appends a new separator to the end of the menu.
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](../../java/lang/IllegalArgumentException.html "class in java.lang")
- when the value of pos
< 0
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](../../java/lang/IllegalArgumentException.html "class in java.lang")
- if the value of pos
< 0
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
Throws:
[IllegalArgumentException](../../java/lang/IllegalArgumentException.html "class in java.lang")
- if the value of pos
< 0
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](../../java/lang/IllegalArgumentException.html "class in java.lang")
- if the value of index
< 0
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](../../java/lang/IllegalArgumentException.html "class in java.lang")
- if the value of pos
< 0
getItemCount
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:
isTearOff
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](../../java/lang/Error.html "class in java.lang")
- if invoked -- this method is not yet implemented
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
remove
public void remove(int pos)
Removes the menu item at the specified index from this menu.
Overrides:
[remove](../../java/awt/Container.html#remove%28int%29)
in class [Container](../../java/awt/Container.html "class in java.awt")
Parameters:
pos
- the position of the item to be removed
Throws:
[IllegalArgumentException](../../java/lang/IllegalArgumentException.html "class in java.lang")
- if the value of pos
< 0, or if pos
is greater than the number of menu items
See Also:
Container.add(java.awt.Component)
remove
public void remove(Component c)
Removes the component c
from this menu.
Overrides:
[remove](../../java/awt/Container.html#remove%28java.awt.Component%29)
in class [Container](../../java/awt/Container.html "class in java.awt")
Parameters:
c
- the component to be removed
See Also:
Container.add(java.awt.Component), Container.remove(int)
removeAll
public void removeAll()
Removes all menu items from this menu.
Overrides:
[removeAll](../../java/awt/Container.html#removeAll%28%29)
in class [Container](../../java/awt/Container.html "class in java.awt")
See Also:
Container.add(java.awt.Component), Container.remove(int)
getMenuComponentCount
public int getMenuComponentCount()
Returns the number of components on the menu.
Returns:
an integer containing the number of components on the menu
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
getMenuComponents
public Component[] getMenuComponents()
Returns an array of Component
s of the menu's subcomponents. Note that this returns all Component
s in the popup menu, including separators.
Returns:
an array of Component
s or an empty array if there is no popup menu
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(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
getPopupMenu
public JPopupMenu getPopupMenu()
Returns the popupmenu associated with this menu. If there is no popupmenu, it will create one.
addMenuListener
public void addMenuListener(MenuListener l)
Adds a listener for menu events.
Parameters:
l
- the listener to be added
removeMenuListener
public void removeMenuListener(MenuListener l)
Removes a listener for menu events.
Parameters:
l
- the listener to be removed
getMenuListeners
public MenuListener[] getMenuListeners()
Returns an array of all the MenuListener
s added to this JMenu with addMenuListener().
Returns:
all of the MenuListener
s added or an empty array if no listeners have been added
Since:
1.4
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](../../java/lang/Error.html "class in java.lang")
- if there is a null
listener
See Also:
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](../../java/lang/Error.html "class in java.lang")
- if there is a null
listener
See Also:
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](../../java/lang/Error.html "class in java.lang")
- if there is a null
listener
See Also:
configurePropertiesFromAction
protected void configurePropertiesFromAction(Action a)
Factory method which sets the ActionEvent
source's properties according to values from theAction
instance. The properties which are set may differ for subclasses. By default, the properties which get set are Text, Icon, Enabled, ToolTipText, ActionCommand
, and Mnemonic
.
Overrides:
[configurePropertiesFromAction](../../javax/swing/JMenuItem.html#configurePropertiesFromAction%28javax.swing.Action%29)
in class [JMenuItem](../../javax/swing/JMenuItem.html "class in javax.swing")
Parameters:
a
- the Action
from which to get the properties, or null
Since:
1.4
See Also:
Action, AbstractButton.setAction(javax.swing.Action)
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:
menuSelectionChanged
public void menuSelectionChanged(boolean isIncluded)
Messaged when the menubar selection changes to activate or deactivate this menu. Overrides JMenuItem.menuSelectionChanged
.
Specified by:
[menuSelectionChanged](../../javax/swing/MenuElement.html#menuSelectionChanged%28boolean%29)
in interface [MenuElement](../../javax/swing/MenuElement.html "interface in javax.swing")
Overrides:
[menuSelectionChanged](../../javax/swing/JMenuItem.html#menuSelectionChanged%28boolean%29)
in class [JMenuItem](../../javax/swing/JMenuItem.html "class in javax.swing")
Parameters:
isIncluded
- true if this menu is active, false if it is not
See Also:
MenuSelectionManager.setSelectedPath(MenuElement[])
getSubElements
public MenuElement[] getSubElements()
Returns an array of MenuElement
s containing the submenu for this menu component. If popup menu is null
returns an empty array. This method is required to conform to theMenuElement
interface. Note that sinceJSeparator
s do not conform to the MenuElement
interface, this array will only contain JMenuItem
s.
Specified by:
[getSubElements](../../javax/swing/MenuElement.html#getSubElements%28%29)
in interface [MenuElement](../../javax/swing/MenuElement.html "interface in javax.swing")
Overrides:
[getSubElements](../../javax/swing/JMenuItem.html#getSubElements%28%29)
in class [JMenuItem](../../javax/swing/JMenuItem.html "class in javax.swing")
Returns:
an array of MenuElement
objects
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](../../javax/swing/MenuElement.html#getComponent%28%29)
in interface [MenuElement](../../javax/swing/MenuElement.html "interface in javax.swing")
Overrides:
[getComponent](../../javax/swing/JMenuItem.html#getComponent%28%29)
in class [JMenuItem](../../javax/swing/JMenuItem.html "class in javax.swing")
Returns:
the Component
that paints this menu item
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](../../java/awt/Container.html#applyComponentOrientation%28java.awt.ComponentOrientation%29)
in class [Container](../../java/awt/Container.html "class in java.awt")
Parameters:
o
- the new component orientation of this menu and the components contained within it.
Throws:
[NullPointerException](../../java/lang/NullPointerException.html "class in java.lang")
- if orientation
is null.
Since:
1.4
See Also:
Component.setComponentOrientation(java.awt.ComponentOrientation), Component.getComponentOrientation()
setComponentOrientation
public void setComponentOrientation(ComponentOrientation o)
Description copied from class: [Component](../../java/awt/Component.html#setComponentOrientation%28java.awt.ComponentOrientation%29)
Sets the language-sensitive orientation that is to be used to order the elements or text within this component. Language-sensitiveLayoutManager
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 toComponentOrientation.UNKNOWN
, indicating that it has not been specified explicitly. The UNKNOWN orientation behaves the same asComponentOrientation.LEFT_TO_RIGHT
.
To set the orientation of a single component, use this method. To set the orientation of an entire component hierarchy, useapplyComponentOrientation.
Overrides:
[setComponentOrientation](../../java/awt/Component.html#setComponentOrientation%28java.awt.ComponentOrientation%29)
in class [Component](../../java/awt/Component.html "class in java.awt")
See Also:
setAccelerator
public void setAccelerator(KeyStroke keyStroke)
setAccelerator
is not defined for JMenu
. Use setMnemonic
instead.
Overrides:
[setAccelerator](../../javax/swing/JMenuItem.html#setAccelerator%28javax.swing.KeyStroke%29)
in class [JMenuItem](../../javax/swing/JMenuItem.html "class in javax.swing")
Parameters:
keyStroke
- the keystroke combination which will invoke the JMenuItem
's actionlisteners without navigating the menu hierarchy
Throws:
[Error](../../java/lang/Error.html "class in java.lang")
- if invoked -- this method is not defined for JMenu. Use setMnemonic
instead
processKeyEvent
protected void processKeyEvent(KeyEvent evt)
Processes key stroke events such as mnemonics and accelerators.
Overrides:
[processKeyEvent](../../javax/swing/JComponent.html#processKeyEvent%28java.awt.event.KeyEvent%29)
in class [JComponent](../../javax/swing/JComponent.html "class in javax.swing")
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()
doClick
public void doClick(int pressTime)
Programmatically performs a "click". This overrides the methodAbstractButton.doClick
in order to make the menu pop up.
Overrides:
[doClick](../../javax/swing/AbstractButton.html#doClick%28int%29)
in class [AbstractButton](../../javax/swing/AbstractButton.html "class in javax.swing")
Parameters:
pressTime
- indicates the number of milliseconds the button was pressed for
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](../../javax/swing/JMenuItem.html#paramString%28%29)
in class [JMenuItem](../../javax/swing/JMenuItem.html "class in javax.swing")
Returns:
a string representation of this JMenu.
getAccessibleContext
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](../../javax/accessibility/Accessible.html#getAccessibleContext%28%29)
in interface [Accessible](../../javax/accessibility/Accessible.html "interface in javax.accessibility")
Overrides:
[getAccessibleContext](../../javax/swing/JMenuItem.html#getAccessibleContext%28%29)
in class [JMenuItem](../../javax/swing/JMenuItem.html "class in javax.swing")
Returns:
an AccessibleJMenu that serves as the AccessibleContext of this JMenu
Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 2004, 2010 Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.