public class JPopupMenu extends JComponentimplements Accessible , MenuElement
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.
| Modifier and Type | Class and Description |
|---|---|
protected class |
JPopupMenu
This class implements accessibility support for the
JPopupMenu class.
|
static class |
JPopupMenu
A popup menu-specific separator.
|
JComponent.AccessibleJComponent Container.AccessibleAWTContainer Component.AccessibleAWTComponent , Component.BaselineResizeBehavior , Component.BltBufferStrategy , Component.FlipBufferStrategy listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWaccessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH| Constructor and Description |
|---|
JPopupMenu()
Constructs a
JPopupMenu without an "invoker".
|
JPopupMenu(String
Constructs a
JPopupMenu with the specified title.
|
| Modifier and Type | Method and Description |
|---|---|
JMenuItem |
add(Action
Appends a new menu item to the end of the menu which dispatches the specified
Action object.
|
JMenuItem |
add(JMenuItem
Appends the specified menu item to the end of this menu.
|
JMenuItem |
add(String
Creates a new menu item with the specified text and appends it to the end of this menu.
|
void |
addMenuKeyListener(MenuKeyListener
Adds a
MenuKeyListener to the popup menu.
|
void |
addPopupMenuListener(PopupMenuListener
Adds a
PopupMenu listener.
|
void |
addSeparator()
Appends a new separator at the end of the menu.
|
protected PropertyChangeListener |
createActionChangeListener(JMenuItem
Returns a properly configured
PropertyChangeListener which updates the control as changes to the
Action occur.
|
protected JMenuItem |
createActionComponent(Action
Factory method which creates the
JMenuItem for
Actions added to the
JPopupMenu.
|
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.
|
AccessibleContext |
getAccessibleContext()
Gets the AccessibleContext associated with this JPopupMenu.
|
Component |
getComponent()
Returns this
JPopupMenu component.
|
Component |
getComponentAtIndex(int i)
Deprecated.
replaced by
Container
|
int |
getComponentIndex(Component
Returns the index of the specified component.
|
static boolean |
getDefaultLightWeightPopupEnabled()
Gets the
defaultLightWeightPopupEnabled property, which by default is
true.
|
Component |
getInvoker()
Returns the component which is the 'invoker' of this popup menu.
|
String |
getLabel()
Returns the popup menu's label
|
Insets |
getMargin()
Returns the margin, in pixels, between the popup menu's border and its containers.
|
MenuKeyListener |
getMenuKeyListeners()
Returns an array of all the
MenuKeyListeners added to this JPopupMenu with addMenuKeyListener().
|
PopupMenuListener |
getPopupMenuListeners()
Returns an array of all the
PopupMenuListeners added to this JMenuItem with addPopupMenuListener().
|
SingleSelectionModel |
getSelectionModel()
Returns the model object that handles single selections.
|
MenuElement |
getSubElements()
Returns an array of
MenuElements containing the submenu for this menu component.
|
PopupMenuUI |
getUI()
Returns the look and feel (L&F) object that renders this component.
|
String |
getUIClassID()
Returns the name of the L&F class that renders this component.
|
void |
insert(Action
Inserts a menu item for the specified
Action object at a given position.
|
void |
insert(Component
Inserts the specified component into the menu at a given position.
|
boolean |
isBorderPainted()
Checks whether the border should be painted.
|
boolean |
isLightWeightPopupEnabled()
Gets the
lightWeightPopupEnabled property.
|
boolean |
isPopupTrigger(MouseEvent
Returns true if the
MouseEvent is considered a popup trigger by the
JPopupMenu's currently installed UI.
|
boolean |
isVisible()
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 |
paintBorder(Graphics
Paints the popup menu's border if the
borderPainted property is
true.
|
protected String |
paramString()
Returns a string representation of this
JPopupMenu.
|
protected void |
processFocusEvent(FocusEvent
Processes focus events occurring on this component by dispatching them to any registered
FocusListener objects.
|
protected void |
processKeyEvent(KeyEvent
Processes key stroke events such as mnemonics and accelerators.
|
void |
processKeyEvent(KeyEvent
Processes a key event forwarded from the
MenuSelectionManager and changes the menu selection, if necessary, by using
MenuSelectionManager's API.
|
void |
processMouseEvent(MouseEvent
This method is required to conform to the
MenuElement interface, but it not implemented.
|
void |
remove(int pos)
Removes the component at the specified index from this popup menu.
|
void |
removeMenuKeyListener(MenuKeyListener
Removes a
MenuKeyListener from the popup menu.
|
void |
removePopupMenuListener(PopupMenuListener
Removes a
PopupMenu listener.
|
void |
setBorderPainted(boolean b)
Sets whether the border should be painted.
|
static void |
setDefaultLightWeightPopupEnabled(boolean aFlag)
Sets the default value of the
lightWeightPopupEnabled property.
|
void |
setInvoker(Component
Sets the invoker of this popup menu -- the component in which the popup menu menu is to be displayed.
|
void |
setLabel(String
Sets the popup menu's label.
|
void |
setLightWeightPopupEnabled(boolean aFlag)
Sets the value of the
lightWeightPopupEnabled property, which by default is
true.
|
void |
setLocation(int x, int y)
Sets the location of the upper left corner of the popup menu using x, y coordinates.
|
void |
setPopupSize(Dimension
Sets the size of the Popup window using a
Dimension object.
|
void |
setPopupSize(int width, int height)
Sets the size of the Popup window to the specified width and height.
|
void |
setSelected(Component
Sets the currently selected component, This will result in a change to the selection model.
|
void |
setSelectionModel(SingleSelectionModel
Sets the model object to handle single selections.
|
void |
setUI(PopupMenuUI
Sets the L&F object that renders this component.
|
void |
setVisible(boolean b)
Sets the visibility of the popup menu.
|
void |
show(Component
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.
|
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, 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, updateadd, 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, validateTreeaction, 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, 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, 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, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCyclepublic JPopupMenu()
JPopupMenu without an "invoker".
public JPopupMenu(Stringlabel)
JPopupMenu with the specified title.
label - the string that a UI may use to display as a title for the popup menu.
public static void setDefaultLightWeightPopupEnabled(boolean aFlag)
lightWeightPopupEnabled property.
aFlag -
true if popups can be lightweight, otherwise
false
getDefaultLightWeightPopupEnabled(),
setLightWeightPopupEnabled(boolean)
public static boolean getDefaultLightWeightPopupEnabled()
defaultLightWeightPopupEnabled property, which by default is
true.
defaultLightWeightPopupEnabled property
setDefaultLightWeightPopupEnabled(boolean)
public PopupMenuUIgetUI()
PopupMenuUI object that renders this component
public void setUI(PopupMenuUIui)
ui - the new
PopupMenuUI L&F object
UIDefaults.getUI(javax.swing.JComponent)
public void updateUI()
updateUI in class
JComponent
JComponent.updateUI()
public StringgetUIClassID()
getUIClassID in class
JComponent
JComponent.getUIClassID() ,
UIDefaults.getUI(javax.swing.JComponent)
protected void processFocusEvent(FocusEventevt)
Component
FocusListener objects.
This method is not called unless focus events are enabled for this component. Focus events are enabled when one of the following occurs:
FocusListener object is registered via addFocusListener. enableEvents. If focus events are enabled for a Component, the current KeyboardFocusManager determines whether or not a focus event should be dispatched to registered FocusListener objects. If the events are to be dispatched, the KeyboardFocusManager calls the Component's dispatchEvent method, which results in a call to the Component's processFocusEvent method.
If focus events are enabled for a Component, calling the Component's dispatchEvent method with a FocusEvent as the argument will result in a call to the Component's processFocusEvent method regardless of the current KeyboardFocusManager.
Note that if the event parameter is null the behavior is unspecified and may result in an exception.
processFocusEvent in class
Component
evt - the focus event
FocusEvent,
FocusListener,
KeyboardFocusManager,
Component.addFocusListener(java.awt.event.FocusListener) ,
Component.enableEvents(long) ,
Component.dispatchEvent(java.awt.AWTEvent)
protected void processKeyEvent(KeyEventevt)
processKeyEvent in class
JComponent
evt - the key event to be processed
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()
public SingleSelectionModelgetSelectionModel()
selectionModel property
SingleSelectionModel
public void setSelectionModel(SingleSelectionModelmodel)
model - the new
SingleSelectionModel
SingleSelectionModel
public JMenuItemadd(JMenuItem menuItem)
menuItem - the
JMenuItem to add
JMenuItem added
public JMenuItemadd(String s)
s - the string for the menu item to be added
public JMenuItemadd(Action a)
Action object.
a - the
Action to add to the menu
Action
protected JMenuItemcreateActionComponent(Action a)
JMenuItem for
Actions added to the
JPopupMenu.
a - the
Action for the menu item to be added
Action
protected PropertyChangeListenercreateActionChangeListener(JMenuItem b)
PropertyChangeListener which updates the control as changes to the
Action occur.
public void remove(int pos)
remove in class
Container
pos - the position of the item to be removed
IllegalArgumentException - if the value of
pos < 0, or if the value of
pos is greater than the number of items
Container.add(java.awt.Component) ,
Container.invalidate() ,
Container.validate() ,
Container.getComponentCount()
public void setLightWeightPopupEnabled(boolean aFlag)
lightWeightPopupEnabled property, which by default is
true. 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.
aFlag -
false to disable lightweight popups
isLightWeightPopupEnabled()
public boolean isLightWeightPopupEnabled()
lightWeightPopupEnabled property.
lightWeightPopupEnabled property
setLightWeightPopupEnabled(boolean)
public StringgetLabel()
setLabel(java.lang.String)
public void setLabel(Stringlabel)
label - a string specifying the label for the popup menu
setLabel(java.lang.String)
public void addSeparator()
public void insert(Actiona, int index)
Action object at a given position.
a - the
Action object to insert
index - specifies the position at which to insert the
Action, where 0 is the first
IllegalArgumentException - if
index < 0
Action
public void insert(Componentcomponent, int index)
component - the
Component to insert
index - specifies the position at which to insert the component, where 0 is the first
IllegalArgumentException - if
index < 0
public void addPopupMenuListener(PopupMenuListenerl)
PopupMenu listener.
l - the
PopupMenuListener to add
public void removePopupMenuListener(PopupMenuListenerl)
PopupMenu listener.
l - the
PopupMenuListener to remove
public PopupMenuListener[] getPopupMenuListeners()
PopupMenuListeners added to this JMenuItem with addPopupMenuListener().
PopupMenuListeners added or an empty array if no listeners have been added
public void addMenuKeyListener(MenuKeyListenerl)
MenuKeyListener to the popup menu.
l - the
MenuKeyListener to be added
public void removeMenuKeyListener(MenuKeyListenerl)
MenuKeyListener from the popup menu.
l - the
MenuKeyListener to be removed
public MenuKeyListener[] getMenuKeyListeners()
MenuKeyListeners added to this JPopupMenu with addMenuKeyListener().
MenuKeyListeners added or an empty array if no listeners have been added
protected void firePopupMenuWillBecomeVisible()
PopupMenuListeners that this popup menu will become visible.
protected void firePopupMenuWillBecomeInvisible()
PopupMenuListeners that this popup menu will become invisible.
protected void firePopupMenuCanceled()
PopupMenuListeners that this popup menu is cancelled.
public void pack()
public void setVisible(boolean b)
setVisible in class
JComponent
b - true to make the popup visible, or false to hide it
Component.isVisible() ,
Component.invalidate()
public boolean isVisible()
isVisible in class
Component
true if the component is visible,
false otherwise
Component.setVisible(boolean)
public void setLocation(int x,
int y)
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.
setLocation in class
Component
x - the x coordinate of the popup's new position in the screen's coordinate space
y - the y coordinate of the popup's new position in the screen's coordinate space
Component.getLocation() ,
Component.setBounds(int, int, int, int) ,
Component.invalidate()
public ComponentgetInvoker()
Component in which the popup menu is displayed
public void setInvoker(Componentinvoker)
invoker - the
Component in which the popup menu is displayed
public void show(Componentinvoker, int x, int y)
invoker - the component in whose space the popup menu is to appear
x - the x coordinate in invoker's coordinate space at which the popup menu is to be displayed
y - the y coordinate in invoker's coordinate space at which the popup menu is to be displayed
@Deprecated public ComponentgetComponentAtIndex(int i)
Container.getComponent(int)
i - the index of the component, where 0 is the first
Component at that index
public int getComponentIndex(Componentc)
c - the
Component to find
public void setPopupSize(Dimensiond)
Dimension object. This is equivalent to
setPreferredSize(d).
d - the
Dimension specifying the new size of this component.
public void setPopupSize(int width,
int height)
setPreferredSize(new Dimension(width, height)).
width - the new width of the Popup in pixels
height - the new height of the Popup in pixels
public void setSelected(Componentsel)
sel - the
Component to select
public boolean isBorderPainted()
setBorderPainted(boolean)
public void setBorderPainted(boolean b)
b - if true, the border is painted.
isBorderPainted()
protected void paintBorder(Graphicsg)
borderPainted property is
true.
paintBorder in class
JComponent
g - the
Graphics object
JComponent.paint(java.awt.Graphics) ,
JComponent.setBorder(javax.swing.border.Border)
public InsetsgetMargin()
Insets object containing the margin values.
protected StringparamString()
JPopupMenu. 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.
paramString in class
JComponent
JPopupMenu.
public AccessibleContextgetAccessibleContext()
getAccessibleContext in interface
Accessible
getAccessibleContext in class
Component
public void processMouseEvent(MouseEventevent, MenuElement [] path, MenuSelectionManager manager)
MenuElement interface, but it not implemented.
processMouseEvent in interface
MenuElement
MenuElement.processMouseEvent(MouseEvent, MenuElement[], MenuSelectionManager)
public void processKeyEvent(KeyEvente, MenuElement [] path, MenuSelectionManager manager)
MenuSelectionManager and changes the menu selection, if necessary, by using
MenuSelectionManager's API.
Note: you do not have to forward the event to sub-components. This is done automatically by the MenuSelectionManager.
processKeyEvent in interface
MenuElement
e - a
KeyEvent
path - the
MenuElement path array
manager - the
MenuSelectionManager
public void menuSelectionChanged(boolean isIncluded)
javax.swing.MenuElement interface. Overrides
MenuElement.menuSelectionChanged.
menuSelectionChanged in interface
MenuElement
isIncluded - true if this menu is active, false if it is not
MenuElement.menuSelectionChanged(boolean)
public MenuElement[] getSubElements()
MenuElements containing the submenu for this menu component. It will only return items conforming to the
JMenuElement interface. If popup menu is
null returns an empty array. This method is required to conform to the
MenuElement interface.
getSubElements in interface
MenuElement
MenuElement objects
MenuElement.getSubElements()
public ComponentgetComponent()
JPopupMenu component.
getComponent in interface
MenuElement
JPopupMenu object
MenuElement.getComponent()
public boolean isPopupTrigger(MouseEvente)
MouseEvent is considered a popup trigger by the
JPopupMenu's currently installed UI.