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

public Class JPopupMenu

extends JComponent
implements Accessible, MenuElement
Class Inheritance
All Implemented Interfaces
javax.swing.MenuElement, javax.accessibility.Accessible
Known Direct Subclasses
javax.swing.plaf.basic.BasicComboPopup, javax.swing.plaf.synth.SynthInternalFrameTitlePane.JPopupMenuUIResource
Annotations
@JavaBean
defaultProperty:UI
description:A small window that pops up and displays a series of choices.
@SwingContainer:false
@SuppressWarnings:serial
Imports
java.awt.AWTEvent, .Component, .Dimension, .Frame, .Graphics, .GraphicsConfiguration, .GraphicsDevice, .GraphicsEnvironment, .Insets, .Point, .Rectangle, .Toolkit, .Window, java.awt.event.FocusEvent, .KeyEvent, .MouseEvent, java.beans.BeanProperty, .JavaBean, .PropertyChangeEvent, .PropertyChangeListener, java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serial, .Serializable, java.util.ArrayList, .Vector, javax.accessibility.Accessible, .AccessibleContext, .AccessibleRole, .AccessibleSelection, .AccessibleState, javax.swing.event.EventListenerList, .MenuKeyEvent, .MenuKeyListener, .PopupMenuEvent, .PopupMenuListener, javax.swing.plaf.PopupMenuUI, javax.swing.plaf.basic.BasicComboPopup, sun.awt.SunToolkit

An implementation of a popup menu -- a small window that pops up and displays a series of choices. A 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 java.beans.XMLEncoder.

Authors
Georges Saab, David Karlton, Arnaud Weber
Since
1.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
JPopupMenu.AccessibleJPopupMenu

This class implements accessibility support for the JPopupMenu class.

private class
public static class
JPopupMenu.Separator

A popup menu-specific separator.

Field Summary

Modifier and TypeField and Description
private static final Object
private static final boolean
private static boolean
private int
private int
pack-priv transient Frame
pack-priv transient Component
private String
private boolean
lightWeightPopup

Used to indicate if lightweight popups should be used.

private Insets
private boolean
pack-priv transient Popup
pack-priv static boolean
popupPositionFixDisabled

Bug#4425878-Property javax.swing.adjustPopupLocationToFit introduced

private PropertyChangeListener
private SingleSelectionModel
private static final boolean
private static final String
private static final boolean
Inherited from javax.swing.JComponent:
DEBUG_GRAPHICS_LOADEDfocusControllerivSourceComponentlistenerListpaintingChildTOOL_TIP_TEXT_KEYuiUNDEFINED_CONDITIONWHEN_ANCESTOR_OF_FOCUSED_COMPONENTWHEN_FOCUSEDWHEN_IN_FOCUSED_WINDOW

Constructor Summary

AccessConstructor and Description
public
JPopupMenu()

Constructs a JPopupMenu without an "invoker".

public
JPopupMenu(String
the string that a UI may use to display as a title for the popup menu.
label
)

Constructs a JPopupMenu with the specified title.

Method Summary

Modifier and TypeMethod and Description
public JMenuItem

Returns:

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

Appends the specified menu item to the end of this menu.

public JMenuItem

Returns:

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

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

public JMenuItem

Returns:

the new menu item
add
(Action
the Action to add to the menu
a
)

Appends a new menu item to the end of the menu which dispatches the specified Action object.

public void
addMenuKeyListener(MenuKeyListener
the MenuKeyListener to be added
l
)

Adds a MenuKeyListener to the popup menu.

public void
addPopupMenuListener(PopupMenuListener
the PopupMenuListener to add
l
)

Adds a PopupMenu listener.

public void
addSeparator()

Appends a new separator at the end of the menu.

pack-priv Point
adjustPopupLocationToFitScreen(int xPosition, int yPosition)

Returns an point which has been adjusted to take into account the desktop bounds, taskbar and multi-monitor configuration.

pack-priv boolean

Returns:

true
alwaysOnTop
()

Overrides javax.swing.JComponent.alwaysOnTop.

Always returns true since popups, by definition, should always be on top of all other windows.

pack-priv static boolean
canPopupOverlapTaskBar()

Returns whether popup is allowed to be shown above the task bar.

protected PropertyChangeListener

Returns:

a properly configured PropertyChangeListener
createActionChangeListener
(JMenuItem
the menu item for which to create a listener
b
)

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

protected JMenuItem

Returns:

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

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

private void
fireMenuKeyPressed(MenuKeyEvent
a MenuKeyEvent
event
)

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

private void
fireMenuKeyReleased(MenuKeyEvent
a MenuKeyEvent
event
)

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

private void
fireMenuKeyTyped(MenuKeyEvent
a MenuKeyEvent
event
)

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

protected void
firePopupMenuCanceled()

Notifies PopupMenuListeners that this popup menu is cancelled.

protected void
firePopupMenuWillBecomeInvisible()

Notifies PopupMenuListeners that this popup menu will become invisible.

protected void
firePopupMenuWillBecomeVisible()

Notifies PopupMenuListeners that this popup menu will become visible.

public AccessibleContext

Returns:

an AccessibleJPopupMenu that serves as the AccessibleContext of this JPopupMenu
getAccessibleContext
()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this JPopupMenu.

public Component

Returns:

this JPopupMenu object
getComponent
()

Implements javax.swing.MenuElement.getComponent.

Returns this JPopupMenu component.

public Component

Returns:

the Component at that index
getComponentAtIndex
(int
the index of the component, where 0 is the first
i
)
Deprecated replaced by java.awt.Container#getComponent(int)

Returns the component at the specified index.

public int

Returns:

the index of the component, where 0 is the first; or -1 if the component is not found
getComponentIndex
(Component
the Component to find
c
)

Returns the index of the specified component.

private GraphicsConfiguration
getCurrentGraphicsConfiguration(Point popupLocation)

Tries to find GraphicsConfiguration that contains the mouse cursor position.

public static boolean

Returns:

the value of the defaultLightWeightPopupEnabled property
getDefaultLightWeightPopupEnabled
()

Gets the defaultLightWeightPopupEnabled property, which by default is true.

private static Frame
public Component

Returns:

the Component in which the popup menu is displayed
getInvoker
()

Returns the component which is the 'invoker' of this popup menu.

public String

Returns:

a string containing the popup menu's label
getLabel
()

Returns the popup menu's label

public Insets

Returns:

an Insets object containing the margin values.
getMargin
()

Returns the margin, in pixels, between the popup menu's border and its containers.

private Window
public MenuKeyListener[]

Returns:

all of the MenuKeyListeners added or an empty array if no listeners have been added
getMenuKeyListeners
()

Returns an array of all the MenuKeyListeners added to this JPopupMenu with addMenuKeyListener().

public PopupMenuListener[]

Returns:

all of the PopupMenuListeners added or an empty array if no listeners have been added
getPopupMenuListeners
()

Returns an array of all the PopupMenuListeners added to this JMenuItem with addPopupMenuListener().

pack-priv JPopupMenu

Returns:

the topmost grandparent JPopupMenu
getRootPopupMenu
()

Returns the popup menu which is at the root of the menu system for this popup menu.

public SingleSelectionModel

Returns:

the selectionModel property
getSelectionModel
()

Returns the model object that handles single selections.

public MenuElement[]

Returns:

an array of MenuElement objects
getSubElements
()

Implements javax.swing.MenuElement.getSubElements.

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

public PopupMenuUI

Returns:

the PopupMenuUI object that renders this component
getUI
()

Overrides javax.swing.JComponent.getUI.

Returns the look and feel (L&F) object that renders this component.

public String

Returns:

the string "PopupMenuUI"
getUIClassID
()

Overrides javax.swing.JComponent.getUIClassID.

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

public void
insert(Action
the Action object to insert
a
,
int
specifies the position at which to insert the Action, where 0 is the first
index
)

Inserts a menu item for the specified Action object at a given position.

public void
insert(Component
the Component to insert
component
,
int
specifies the position at which to insert the component, where 0 is the first
index
)

Inserts the specified component into the menu at a given position.

public boolean

Returns:

true if the border is painted, false otherwise
isBorderPainted
()

Checks whether the border should be painted.

public boolean

Returns:

the value of the lightWeightPopupEnabled property
isLightWeightPopupEnabled
()

Gets the lightWeightPopupEnabled property.

private boolean

Returns:

true if this menu is a standalone popup menu, otherwise false
isPopupMenu
()

Returns true if the popup menu is a standalone popup menu rather than the submenu of a JMenu.

public boolean

Returns:

true if the mouse event is a popup trigger
isPopupTrigger
(MouseEvent
a MouseEvent
e
)

Returns true if the MouseEvent is considered a popup trigger by the JPopupMenu's currently installed UI.

pack-priv boolean

Returns:

true if popup
isSubPopupMenu
(JPopupMenu
a JPopupMenu
popup
)

Examines the list of menu items to determine whether popup is a popup menu.

public boolean
isVisible()

Overrides java.awt.Component.isVisible.

Returns true if the popup menu is visible (currently being displayed).

public void
pack()

Lays out the container so that it uses the minimum space needed to display its contents.

protected void
paintBorder(Graphics
the Graphics object
g
)

Overrides javax.swing.JComponent.paintBorder.

Paints the popup menu's border if the borderPainted property is true.

protected String

Returns:

a string representation of this JPopupMenu.
paramString
()

Overrides javax.swing.JComponent.paramString.

Returns a string representation of this JPopupMenu.

protected void
processFocusEvent(FocusEvent evt)

Overrides java.awt.Component.processFocusEvent.

Processes focus events occurring on this component by dispatching them to any registered FocusListener objects.

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

Overrides javax.swing.JComponent.processKeyEvent.

Processes key stroke events such as mnemonics and accelerators.

public void
processKeyEvent(KeyEvent
a KeyEvent
e
,
MenuElement[]
the MenuElement path array
path
,
MenuSelectionManager
the MenuSelectionManager
manager
)

Implements javax.swing.MenuElement.processKeyEvent.

Processes a key event forwarded from the MenuSelectionManager and changes the menu selection, if necessary, by using MenuSelectionManager's API.

private void
processMenuKeyEvent(MenuKeyEvent
a MenuKeyEvent object
e
)

Handles a keystroke in a menu.

public void
processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)

Implements javax.swing.MenuElement.processMouseEvent.

This method is required to conform to the MenuElement interface, but it not implemented.

private void
readObject(ObjectInputStream s)

Hides javax.swing.JComponent.readObject.

We use the ObjectInputStream "registerValidation" callback to update the UI for the entire tree of components after they've all been read in.

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

Overrides java.awt.Container.remove.

Removes the component at the specified index from this popup menu.

public void
removeMenuKeyListener(MenuKeyListener
the MenuKeyListener to be removed
l
)

Removes a MenuKeyListener from the popup menu.

public void
removePopupMenuListener(PopupMenuListener
the PopupMenuListener to remove
l
)

Removes a PopupMenu listener.

public void
setBorderPainted(boolean
if true, the border is painted.
b
)

Sets whether the border should be painted.

public static void
setDefaultLightWeightPopupEnabled(boolean
true if popups can be lightweight, otherwise false
aFlag
)

Sets the default value of the lightWeightPopupEnabled property.

public void
setInvoker(Component
the Component in which the popup menu is displayed
invoker
)

Sets the invoker of this popup menu -- the component in which the popup menu is to be displayed.

public void
setLabel(String
a string specifying the label for the popup menu
label
)

Sets the popup menu's label.

public void
setLightWeightPopupEnabled(boolean
false to disable lightweight popups
aFlag
)

Sets the value of the lightWeightPopupEnabled property, which by default is true.

public void
setLocation(int
the x coordinate of the popup's new position in the screen's coordinate space
x
,
int
the y coordinate of the popup's new position in the screen's coordinate space
y
)

Overrides java.awt.Component.setLocation.

Sets the location of the upper left corner of the popup menu using x, y coordinates.

public void
setPopupSize(Dimension
the Dimension specifying the new size of this component.
d
)

Sets the size of the Popup window using a Dimension object.

public void
setPopupSize(int
the new width of the Popup in pixels
width
,
int
the new height of the Popup in pixels
height
)

Sets the size of the Popup window to the specified width and height.

public void
setSelected(Component
the Component to select
sel
)

Sets the currently selected component, This will result in a change to the selection model.

public void
setSelectionModel(SingleSelectionModel
the new SingleSelectionModel
model
)

Sets the model object to handle single selections.

public void
setUI(PopupMenuUI
the new PopupMenuUI L&F object
ui
)

Sets the L&F object that renders this component.

public void
setVisible(boolean
true to make the popup visible, or false to hide it
b
)

Overrides javax.swing.JComponent.setVisible.

Sets the visibility of the popup menu.

public void
show(Component
the component in whose space the popup menu is to appear
invoker
,
int
the x coordinate in invoker's coordinate space at which the popup menu is to be displayed
x
,
int
the y coordinate in invoker's coordinate space at which the popup menu is to be displayed
y
)

Displays the popup menu at the position x,y in the coordinate space of the component invoker.

private void
showPopup()

Retrieves Popup instance from the PopupMenuUI that has had show invoked on it.

public void
updateUI()

Overrides javax.swing.JComponent.updateUI.

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

private void
writeObject(ObjectOutputStream s)

Hides javax.swing.JComponent.writeObject.

Before writing a JComponent to an ObjectOutputStream we temporarily uninstall its UI.

Inherited from javax.swing.JComponent:
_paintImmediatelyaddAncestorListeneraddNotifyaddVetoableChangeListenercheckIfChildObscuredBySiblingclientPropertyChangedcomponentInputMapChangedcomputeVisibleRectcomputeVisibleRectcompWriteObjectNotifycontainscreateToolTipdisabledndDonedropLocationForPointenablefirePropertyChangefirePropertyChangefirePropertyChangefireVetoableChangegetActionForKeyStrokegetActionMapgetActionMapgetAlignmentXgetAlignmentYgetAncestorListenersgetAutoscrollsgetBaselinegetBaselineResizeBehaviorgetBordergetBoundsgetClientPropertygetComponentGraphicsgetComponentPopupMenugetConditionForKeyStrokegetCreatedDoubleBuffergetDebugGraphicsOptionsgetDefaultLocalegetFontMetricsgetGraphicsgetGraphicsInvokedgetHeightgetInheritsPopupMenugetInputMapgetInputMapgetInputMapgetInputVerifiergetInsetsgetInsetsgetListenersgetLocationgetManagingFocusBackwardTraversalKeysgetManagingFocusForwardTraversalKeysgetMaximumSizegetMinimumSizegetNextFocusableComponentgetPopupLocationgetPreferredSizegetRegisteredKeyStrokesgetRootPanegetSizegetToolTipLocationgetToolTipTextgetToolTipTextgetTopLevelAncestorgetTransferHandlergetVerifyInputWhenFocusTargetgetVetoableChangeListenersgetVisibleRectgetWidthgetWriteObjCountergetXgetYgrabFocushideisDoubleBufferedisLightweightComponentisManagingFocusisOpaqueisOptimizedDrawingEnabledisPaintingisPaintingForPrintisPaintingOriginisPaintingTileisRequestFocusEnabledisValidateRootpaintpaintChildrenpaintComponentpaintForceDoubleBufferedpaintImmediatelypaintImmediatelypaintToOffscreenprintprintAllprintBorderprintChildrenprintComponentprocessComponentKeyEventprocessKeyBindingprocessKeyBindingsprocessKeyBindingsForAllComponentsprocessMouseEventprocessMouseMotionEventputClientPropertyrectangleIsObscuredregisterKeyboardActionregisterKeyboardActionremoveAncestorListenerremoveNotifyremoveVetoableChangeListenerrepaintrepaintrequestDefaultFocusrequestFocusrequestFocusrequestFocusInWindowrequestFocusInWindowresetKeyboardActionsreshaperevalidatesafelyGetGraphicssafelyGetGraphicsscrollRectToVisiblesetActionMapsetAlignmentXsetAlignmentYsetAutoscrollssetBackgroundsetBordersetComponentPopupMenusetCreatedDoubleBuffersetDebugGraphicsOptionssetDefaultLocalesetDoubleBufferedsetDropLocationsetEnabledsetFocusTraversalKeyssetFontsetForegroundsetInheritsPopupMenusetInputMapsetInputVerifiersetMaximumSizesetMinimumSizesetNextFocusableComponentsetOpaquesetPaintingChildsetPreferredSizesetRequestFocusEnabledsetToolTipTextsetTransferHandlersetUIsetUIPropertysetVerifyInputWhenFocusTargetsetWriteObjCountershouldDebugGraphicssuperProcessMouseMotionEventunregisterKeyboardActionupdate