- All Implemented Interfaces:
ImageObserver
,MenuContainer
,Serializable
,Accessible
,MenuElement
- Direct Known Subclasses:
BasicComboPopup
JPopupMenu
is used for the
menu that appears when the user selects an item on the menu bar.
It is also used for "pull-right" menu that appears when the
selects a menu item that activates it. Finally, a JPopupMenu
can also be used anywhere else you want a menu to appear. For
example, when the user right-clicks in a specified area.
For information and examples of using popup menus, see How to Use Menus 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
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected class
This class implements accessibility support for theJPopupMenu
class.static class
A popup menu-specific separator.Nested classes/interfaces declared in class javax.swing.JComponent
JComponent.AccessibleJComponent
Nested classes/interfaces declared in class java.awt.Container
Container.AccessibleAWTContainer
Nested classes/interfaces declared in class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
Field Summary
Fields declared in class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Fields declared in class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
Fields declared in interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
-
Constructor Summary
ConstructorDescriptionConstructs aJPopupMenu
without an "invoker".JPopupMenu
(String label) Constructs aJPopupMenu
with the specified title. -
Method Summary
Modifier and TypeMethodDescriptionCreates a new menu item with the specified text and appends it to the end of this menu.Appends a new menu item to the end of the menu which dispatches the specifiedAction
object.Appends the specified menu item to the end of this menu.void
Adds aMenuKeyListener
to the popup menu.void
Adds aPopupMenu
listener.void
Appends a new separator at the end of the menu.protected PropertyChangeListener
Returns a properly configuredPropertyChangeListener
which updates the control as changes to theAction
occur.protected JMenuItem
Factory method which creates theJMenuItem
forActions
added to theJPopupMenu
.protected void
NotifiesPopupMenuListeners
that this popup menu is cancelled.protected void
NotifiesPopupMenuListener
s that this popup menu will become invisible.protected void
NotifiesPopupMenuListener
s that this popup menu will become visible.Gets the AccessibleContext associated with this JPopupMenu.Returns thisJPopupMenu
component.getComponentAtIndex
(int i) Deprecated.int
Returns the index of the specified component.static boolean
Gets thedefaultLightWeightPopupEnabled
property, which by default istrue
.Returns the component which is the 'invoker' of this popup menu.getLabel()
Returns the popup menu's labelReturns the margin, in pixels, between the popup menu's border and its containers.Returns an array of all theMenuKeyListener
s added to this JPopupMenu with addMenuKeyListener().Returns an array of all thePopupMenuListener
s added to this JMenuItem with addPopupMenuListener().Returns the model object that handles single selections.Returns an array ofMenuElement
s containing the submenu for this menu component.getUI()
Returns the look and feel (L&F) object that renders this component.Returns the name of the L&F class that renders this component.void
Inserts the specified component into the menu at a given position.void
Inserts a menu item for the specifiedAction
object at a given position.boolean
Checks whether the border should be painted.boolean
Gets thelightWeightPopupEnabled
property.boolean
Returns true if theMouseEvent
is considered a popup trigger by theJPopupMenu
's currently installed UI.boolean
Returns true if the popup menu is visible (currently being displayed).void
menuSelectionChanged
(boolean isIncluded) Messaged when the menubar selection changes to activate or deactivate this menu.void
pack()
Lays out the container so that it uses the minimum space needed to display its contents.protected void
Paints the popup menu's border if theborderPainted
property istrue
.protected String
Returns a string representation of thisJPopupMenu
.protected void
processKeyEvent
(KeyEvent evt) Processes key stroke events such as mnemonics and accelerators.void
processKeyEvent
(KeyEvent e, MenuElement[] path, MenuSelectionManager manager) Processes a key event forwarded from theMenuSelectionManager
and changes the menu selection, if necessary, by usingMenuSelectionManager
's API.void
processMouseEvent
(MouseEvent event, MenuElement[] path, MenuSelectionManager manager) This method is required to conform to theMenuElement
interface, but it not implemented.void
remove
(int pos) Removes the component at the specified index from this popup menu.void
Removes aMenuKeyListener
from the popup menu.void
Removes aPopupMenu
listener.void
setBorderPainted
(boolean b) Sets whether the border should be painted.static void
setDefaultLightWeightPopupEnabled
(boolean aFlag) Sets the default value of thelightWeightPopupEnabled
property.void
setInvoker
(Component invoker) Sets the invoker of this popup menu -- the component in which the popup menu is to be displayed.void
Sets the popup menu's label.void
setLightWeightPopupEnabled
(boolean aFlag) Sets the value of thelightWeightPopupEnabled
property, which by default istrue
.void
setLocation
(int x, int y) Sets the location of the upper left corner of the popup menu using x, y coordinates.void
setPopupSize
(int width, int height) Sets the size of the Popup window to the specified width and height.void
Sets the size of the Popup window using aDimension
object.void
setSelected
(Component sel) Sets the currently selected component, This will result in a change to the selection model.void
Sets the model object to handle single selections.void
setUI
(PopupMenuUI ui) Sets the L&F object that renders this component.void
setVisible
(boolean b) Sets the visibility of the popup menu.void
Displays the popup menu at the position x,y in the coordinate space of the component invoker.void
updateUI()
Resets the UI property to a value from the current look and feel.Methods declared in class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, 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, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, unregisterKeyboardAction, update
Methods declared in class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
Methods declared in class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, 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, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
Constructor Details
-
JPopupMenu
public JPopupMenu()Constructs aJPopupMenu
without an "invoker". -
JPopupMenu
Constructs aJPopupMenu
with the specified title.- Parameters:
label
- the string that a UI may use to display as a title for the popup menu.
-
-
Method Details
-
setDefaultLightWeightPopupEnabled
public static void setDefaultLightWeightPopupEnabled(boolean aFlag) Sets the default value of thelightWeightPopupEnabled
property.- Parameters:
aFlag
-true
if popups can be lightweight, otherwisefalse
- See Also:
-
getDefaultLightWeightPopupEnabled
public static boolean getDefaultLightWeightPopupEnabled()Gets thedefaultLightWeightPopupEnabled
property, which by default istrue
.- Returns:
- the value of the
defaultLightWeightPopupEnabled
property - See Also:
-
getUI
Returns the look and feel (L&F) object that renders this component.- Overrides:
getUI
in classJComponent
- Returns:
- the
PopupMenuUI
object that renders this component
-
setUI
@BeanProperty(hidden=true, visualUpdate=true, description="The UI object that implements the Component\'s LookAndFeel.") public void setUI(PopupMenuUI ui) Sets the L&F object that renders this component.- Parameters:
ui
- the newPopupMenuUI
L&F object- See Also:
-
updateUI
public void updateUI()Resets the UI property to a value from the current look and feel.- Overrides:
updateUI
in classJComponent
- See Also:
-
getUIClassID
Returns the name of the L&F class that renders this component.- Overrides:
getUIClassID
in classJComponent
- Returns:
- the string "PopupMenuUI"
- See Also:
-
processKeyEvent
Processes key stroke events such as mnemonics and accelerators.- Overrides:
processKeyEvent
in classJComponent
- Parameters:
evt
- the key event to be processed- See Also:
-
getSelectionModel
Returns the model object that handles single selections.- Returns:
- the
selectionModel
property - See Also:
-
setSelectionModel
@BeanProperty(bound=false, expert=true, description="The selection model for the popup menu") public void setSelectionModel(SingleSelectionModel model) Sets the model object to handle single selections.- Parameters:
model
- the newSingleSelectionModel
- See Also:
-
add
-
add
-
add
-
createActionComponent
-
createActionChangeListener
Returns a properly configuredPropertyChangeListener
which updates the control as changes to theAction
occur.- Parameters:
b
- the menu item for which to create a listener- Returns:
- a properly configured
PropertyChangeListener
-
remove
public void remove(int pos) Removes the component at the specified index from this popup menu.- Overrides:
remove
in classContainer
- Parameters:
pos
- the position of the item to be removed- Throws:
IllegalArgumentException
- if the value ofpos
< 0, or if the value ofpos
is greater than the number of items- See Also:
-
setLightWeightPopupEnabled
@BeanProperty(bound=false, expert=true, description="Determines whether lightweight popups are used when possible") public void setLightWeightPopupEnabled(boolean aFlag) Sets the value of thelightWeightPopupEnabled
property, which by default istrue
. By default, when a look and feel displays a popup, it can choose to use a lightweight (all-Java) popup. Lightweight popup windows are more efficient than heavyweight (native peer) windows, but lightweight and heavyweight components do not mix well in a GUI. If your application mixes lightweight and heavyweight components, you should disable lightweight popups. Some look and feels might always use heavyweight popups, no matter what the value of this property.- Parameters:
aFlag
-false
to disable lightweight popups- See Also:
-
isLightWeightPopupEnabled
public boolean isLightWeightPopupEnabled()Gets thelightWeightPopupEnabled
property.- Returns:
- the value of the
lightWeightPopupEnabled
property - See Also:
-
getLabel
Returns the popup menu's label- Returns:
- a string containing the popup menu's label
- See Also:
-
setLabel
Sets the popup menu's label. Different look and feels may choose to display or not display this.- Parameters:
label
- a string specifying the label for the popup menu- See Also:
-
addSeparator
public void addSeparator()Appends a new separator at the end of the menu. -
insert
Inserts a menu item for the specifiedAction
object at a given position.- Parameters:
a
- theAction
object to insertindex
- specifies the position at which to insert theAction
, where 0 is the first- Throws:
IllegalArgumentException
- ifindex
< 0- See Also:
-
insert
Inserts the specified component into the menu at a given position.- Parameters:
component
- theComponent
to insertindex
- specifies the position at which to insert the component, where 0 is the first- Throws:
IllegalArgumentException
- ifindex
< 0
-
addPopupMenuListener
Adds aPopupMenu
listener.- Parameters:
l
- thePopupMenuListener
to add
-
removePopupMenuListener
Removes aPopupMenu
listener.- Parameters:
l
- thePopupMenuListener
to remove
-
getPopupMenuListeners
Returns an array of all thePopupMenuListener
s added to this JMenuItem with addPopupMenuListener().- Returns:
- all of the
PopupMenuListener
s added or an empty array if no listeners have been added - Since:
- 1.4
-
addMenuKeyListener
Adds aMenuKeyListener
to the popup menu.- Parameters:
l
- theMenuKeyListener
to be added- Since:
- 1.5
-
removeMenuKeyListener
Removes aMenuKeyListener
from the popup menu.- Parameters:
l
- theMenuKeyListener
to be removed- Since:
- 1.5
-
getMenuKeyListeners
Returns an array of all theMenuKeyListener
s added to this JPopupMenu with addMenuKeyListener().- Returns:
- all of the
MenuKeyListener
s added or an empty array if no listeners have been added - Since:
- 1.5
-
firePopupMenuWillBecomeVisible
protected void firePopupMenuWillBecomeVisible()NotifiesPopupMenuListener
s that this popup menu will become visible. -
firePopupMenuWillBecomeInvisible
protected void firePopupMenuWillBecomeInvisible()NotifiesPopupMenuListener
s that this popup menu will become invisible. -
firePopupMenuCanceled
protected void firePopupMenuCanceled()NotifiesPopupMenuListeners
that this popup menu is cancelled. -
pack
public void pack()Lays out the container so that it uses the minimum space needed to display its contents. -
setVisible
Sets the visibility of the popup menu.- Overrides:
setVisible
in classJComponent
- Parameters:
b
- true to make the popup visible, or false to hide it- See Also:
-
isVisible
-
setLocation
Sets the location of the upper left corner of the popup menu using x, y coordinates.The method changes the geometry-related data. Therefore, the native windowing system may ignore such requests, or it may modify the requested data, so that the
JPopupMenu
object is placed and sized in a way that corresponds closely to the desktop settings.- Overrides:
setLocation
in classComponent
- Parameters:
x
- the x coordinate of the popup's new position in the screen's coordinate spacey
- the y coordinate of the popup's new position in the screen's coordinate space- See Also:
-
getInvoker
Returns the component which is the 'invoker' of this popup menu.- Returns:
- the
Component
in which the popup menu is displayed
-
setInvoker
@BeanProperty(bound=false, expert=true, description="The invoking component for the popup menu") public void setInvoker(Component invoker) Sets the invoker of this popup menu -- the component in which the popup menu is to be displayed.- Parameters:
invoker
- theComponent
in which the popup menu is displayed
-
show
Displays the popup menu at the position x,y in the coordinate space of the component invoker.- Parameters:
invoker
- the component in whose space the popup menu is to appearx
- the x coordinate in invoker's coordinate space at which the popup menu is to be displayedy
- the y coordinate in invoker's coordinate space at which the popup menu is to be displayed
-
getComponentAtIndex
Deprecated.replaced byContainer.getComponent(int)
Returns the component at the specified index.- Parameters:
i
- the index of the component, where 0 is the first- Returns:
- the
Component
at that index
-
getComponentIndex
Returns the index of the specified component.- Parameters:
c
- theComponent
to find- Returns:
- the index of the component, where 0 is the first; or -1 if the component is not found
-
setPopupSize
Sets the size of the Popup window using aDimension
object. This is equivalent tosetPreferredSize(d)
.- Parameters:
d
- theDimension
specifying the new size of this component.
-
setPopupSize
@BeanProperty(description="The size of the popup menu") public void setPopupSize(int width, int height) Sets the size of the Popup window to the specified width and height. This is equivalent tosetPreferredSize(new Dimension(width, height))
.- Parameters:
width
- the new width of the Popup in pixelsheight
- the new height of the Popup in pixels
-
setSelected
@BeanProperty(expert=true, hidden=true, description="The selected component on the popup menu") public void setSelected(Component sel) Sets the currently selected component, This will result in a change to the selection model.- Parameters:
sel
- theComponent
to select
-
isBorderPainted
public boolean isBorderPainted()Checks whether the border should be painted.- Returns:
- true if the border is painted, false otherwise
- See Also:
-
setBorderPainted
@BeanProperty(bound=false, description="Is the border of the popup menu painted") public void setBorderPainted(boolean b) Sets whether the border should be painted.- Parameters:
b
- if true, the border is painted.- See Also:
-
paintBorder
Paints the popup menu's border if theborderPainted
property istrue
.- Overrides:
paintBorder
in classJComponent
- Parameters:
g
- theGraphics
object- See Also:
-
getMargin
Returns the margin, in pixels, between the popup menu's border and its containers.- Returns:
- an
Insets
object containing the margin values.
-
paramString
Returns a string representation of thisJPopupMenu
. 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 benull
.- Overrides:
paramString
in classJComponent
- Returns:
- a string representation of this
JPopupMenu
.
-
getAccessibleContext
Gets the AccessibleContext associated with this JPopupMenu. For JPopupMenus, the AccessibleContext takes the form of an AccessibleJPopupMenu. A new AccessibleJPopupMenu instance is created if necessary.- Specified by:
getAccessibleContext
in interfaceAccessible
- Overrides:
getAccessibleContext
in classComponent
- Returns:
- an AccessibleJPopupMenu that serves as the AccessibleContext of this JPopupMenu
-
processMouseEvent
This method is required to conform to theMenuElement
interface, but it not implemented.- Specified by:
processMouseEvent
in interfaceMenuElement
- Parameters:
event
- aMouseEvent
to be processedpath
- the path of the receiving element in the menu hierarchymanager
- theMenuSelectionManager
for the menu hierarchy- See Also:
-
processKeyEvent
Processes a key event forwarded from theMenuSelectionManager
and changes the menu selection, if necessary, by usingMenuSelectionManager
's API.Note: you do not have to forward the event to sub-components. This is done automatically by the
MenuSelectionManager
.- Specified by:
processKeyEvent
in interfaceMenuElement
- Parameters:
e
- aKeyEvent
path
- theMenuElement
path arraymanager
- theMenuSelectionManager
-
getSubElements
Returns an array ofMenuElement
s containing the submenu for this menu component. It will only return items conforming to theJMenuElement
interface. If popup menu isnull
returns an empty array. This method is required to conform to theMenuElement
interface.- Specified by:
getSubElements
in interfaceMenuElement
- Returns:
- an array of
MenuElement
objects - See Also:
-
getComponent
Returns thisJPopupMenu
component.- Specified by:
getComponent
in interfaceMenuElement
- Returns:
- this
JPopupMenu
object - See Also:
-
isPopupTrigger
Returns true if theMouseEvent
is considered a popup trigger by theJPopupMenu
's currently installed UI.- Parameters:
e
- aMouseEvent
- Returns:
- true if the mouse event is a popup trigger
- Since:
- 1.3
-
Container.getComponent(int)