| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
JPopupMenu popup = new JPopupMenu();
Based on 40 examples
public class JPopupMenu extends JComponent implements Accessible, MenuElement
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 JavaBeansTM
has been added to the java.beans
package.
Please see {@link java.beans.XMLEncoder}.
Nested Class Summary | |
---|---|
protected class |
This class implements accessibility support for the JPopupMenu class. |
static class |
A popup menu-specific separator. |
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.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy |
Field Summary |
---|
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 |
Constructor Summary | |
---|---|
Constructs a JPopupMenu without an "invoker".
|
|
JPopupMenu(String label) Constructs a JPopupMenu with the specified title.
|
Method Summary | |
---|---|
JMenuItem |
Appends a new menu item to the end of the menu which dispatches the specified Action object.
|
JMenuItem |
Appends the specified menu item to the end of this menu. |
JMenuItem |
Creates a new menu item with the specified text and appends it to the end of this menu. |
void |
Adds a MenuKeyListener to the popup menu.
|
void |
Adds a PopupMenu listener.
|
void |
Appends a new separator at the end of the menu. |
protected PropertyChangeListener |
Returns a properly configured PropertyChangeListener
which updates the control as changes to the Action occur.
|
protected JMenuItem |
Factory method which creates the JMenuItem for
Actions added to the JPopupMenu .
|
protected void |
Notifies PopupMenuListeners that this popup menu is
cancelled.
|
protected void |
Notifies PopupMenuListener s that this popup menu will
become invisible.
|
protected void |
Notifies PopupMenuListener s that this popup menu will
become visible.
|
AccessibleContext |
Gets the AccessibleContext associated with this JPopupMenu. |
Component |
Returns this JPopupMenu component.
|
Component |
getComponentAtIndex(int i) Returns the component at the specified index. |
int |
Returns the index of the specified component. |
static boolean |
Gets the defaultLightWeightPopupEnabled property,
which by default is true .
|
Component |
Returns the component which is the 'invoker' of this popup menu. |
String |
getLabel() Returns the popup menu's label |
Insets |
Returns the margin, in pixels, between the popup menu's border and its containees. |
MenuKeyListener[] |
Returns an array of all the MenuKeyListener s added
to this JPopupMenu with addMenuKeyListener().
|
PopupMenuListener[] |
Returns an array of all the PopupMenuListener s added
to this JMenuItem with addPopupMenuListener().
|
SingleSelectionModel |
Returns the model object that handles single selections. |
MenuElement[] |
Returns an array of MenuElement s containing the submenu
for this menu component.
|
PopupMenuUI |
getUI() Returns the look and feel (L&F) object that renders this component. |
String |
Returns the name of the L&F class that renders this component. |
void |
Inserts a menu item for the specified Action object at
a given position.
|
void |
Inserts the specified component into the menu at a given position. |
boolean |
Checks whether the border should be painted. |
boolean |
Gets the lightWeightPopupEnabled property.
|
boolean |
Returns true if the MouseEvent is considered a popup trigger
by the JPopupMenu '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 the borderPainted
property is true .
|
protected String |
Returns a string representation of this JPopupMenu .
|
protected void |
Processes focus events occurring on this component by dispatching them to any registered FocusListener objects.
|
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 the MenuSelectionManager and changes the menu selection,
if necessary, by using MenuSelectionManager 's API.
|
void |
processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager) 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 |
Removes a MenuKeyListener from the popup menu.
|
void |
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 invoker) Sets the invoker of this popup menu -- the component in which the popup menu menu is to be displayed. |
void |
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 |
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 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 inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public JPopupMenu()
JPopupMenu
without an "invoker".
public JPopupMenu(String label)
JPopupMenu
with the specified title.
label
- the string that a UI may use to display as a title
for the popup menu.Method Detail |
---|
public JMenuItem add(Action a)
Action
object.
a
- the Action
to add to the menupublic JMenuItem add(JMenuItem menuItem)
menuItem
- the JMenuItem
to addJMenuItem
addedpublic JMenuItem add(String s)
s
- the string for the menu item to be addedpublic void addMenuKeyListener(MenuKeyListener l)
MenuKeyListener
to the popup menu.
l
- the MenuKeyListener
to be addedpublic void addPopupMenuListener(PopupMenuListener l)
PopupMenu
listener.
l
- the PopupMenuListener
to addpublic void addSeparator()
protected PropertyChangeListener createActionChangeListener(JMenuItem b)
PropertyChangeListener
which updates the control as changes to the Action
occur.
b
protected JMenuItem createActionComponent(Action a)
JMenuItem
for
Actions
added to the JPopupMenu
.
a
- the Action
for the menu item to be addedprotected void firePopupMenuCanceled()
PopupMenuListeners
that this popup menu is
cancelled.
protected void firePopupMenuWillBecomeInvisible()
PopupMenuListener
s that this popup menu will
become invisible.
protected void firePopupMenuWillBecomeVisible()
PopupMenuListener
s that this popup menu will
become visible.
public AccessibleContext getAccessibleContext()
getAccessibleContext
in class JComponent
public Component getComponent()
JPopupMenu
component.
JPopupMenu
objectpublic Component getComponentAtIndex(int i)
i
- the index of the component, where 0 is the firstComponent
at that indexpublic int getComponentIndex(Component c)
c
- the Component
to findpublic static boolean getDefaultLightWeightPopupEnabled()
defaultLightWeightPopupEnabled
property,
which by default is true
.
defaultLightWeightPopupEnabled
propertypublic Component getInvoker()
Component
in which the popup menu is displayedpublic String getLabel()
public Insets getMargin()
Insets
object containing the margin values.public MenuKeyListener[] getMenuKeyListeners()
MenuKeyListener
s added
to this JPopupMenu with addMenuKeyListener().
MenuKeyListener
s added or an empty
array if no listeners have been addedpublic PopupMenuListener[] getPopupMenuListeners()
PopupMenuListener
s added
to this JMenuItem with addPopupMenuListener().
PopupMenuListener
s added or an empty
array if no listeners have been addedpublic SingleSelectionModel getSelectionModel()
selectionModel
propertypublic MenuElement[] getSubElements()
MenuElement
s 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.
MenuElement
objectspublic PopupMenuUI getUI()
PopupMenuUI
object that renders this componentpublic String getUIClassID()
getUIClassID
in class JComponent
public void insert(Action a, int index)
Action
object at
a given position.
a
- the Action
object to insertindex
- specifies the position at which to insert the
Action
, where 0 is the firstpublic void insert(Component component, int index)
component
- the Component
to insertindex
- specifies the position at which
to insert the component, where 0 is the firstpublic boolean isBorderPainted()
public boolean isLightWeightPopupEnabled()
lightWeightPopupEnabled
property.
lightWeightPopupEnabled
propertypublic boolean isPopupTrigger(MouseEvent e)
MouseEvent
is considered a popup trigger
by the JPopupMenu
's currently installed UI.
e
public boolean isVisible()
isVisible
in class Component
public void menuSelectionChanged(boolean isIncluded)
javax.swing.MenuElement
interface.
Overrides MenuElement.menuSelectionChanged
.
isIncluded
- true if this menu is active, false if
it is notpublic void pack()
protected void paintBorder(Graphics g)
borderPainted
property is true
.
paintBorder
in class JComponent
g
- the Graphics
objectprotected String paramString()
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
.protected void processFocusEvent(FocusEvent evt)
processFocusEvent
in class Component
evt
protected void processKeyEvent(KeyEvent evt)
processKeyEvent
in class JComponent
evt
- the key event to be processedpublic void processKeyEvent(KeyEvent e, 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
.
e
- a KeyEvent
path
- the MenuElement
path arraymanager
- the MenuSelectionManager
public void processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)
MenuElement
interface, but it not implemented.
event
path
manager
public void remove(int pos)
remove
in class Container
pos
- the position of the item to be removedpublic void removeMenuKeyListener(MenuKeyListener l)
MenuKeyListener
from the popup menu.
l
- the MenuKeyListener
to be removedpublic void removePopupMenuListener(PopupMenuListener l)
PopupMenu
listener.
l
- the PopupMenuListener
to removepublic void setBorderPainted(boolean b)
b
- if true, the border is painted.public static void setDefaultLightWeightPopupEnabled(boolean aFlag)
lightWeightPopupEnabled
property.
aFlag
- true
if popups can be lightweight,
otherwise false
public void setInvoker(Component invoker)
invoker
- the Component
in which the popup
menu is displayedpublic void setLabel(String label)
label
- a string specifying the label for the popup menupublic 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 popupspublic void setLocation(int x, int y)
setLocation
in class Component
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 spacepublic void setPopupSize(Dimension d)
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 pixelsheight
- the new height of the Popup in pixelspublic void setSelected(Component sel)
sel
- the Component
to selectpublic void setSelectionModel(SingleSelectionModel model)
model
- the new SingleSelectionModel
public void setUI(PopupMenuUI ui)
ui
- the new PopupMenuUI
L&F objectpublic void setVisible(boolean b)
setVisible
in class JComponent
b
- true to make the popup visible, or false to
hide itpublic void show(Component invoker, int x, int y)
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 displayedpublic void updateUI()
updateUI
in class JComponent
| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |