|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.awt.Component java.awt.Container javax.swing.JComponent javax.swing.JPopupMenu
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.
For the keyboard keys used by this component in the standard Look and
Feel (L&F) renditions, see the
JPopupMenu
key assignments.
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
.
Nested Class Summary | |
protected class |
JPopupMenu.AccessibleJPopupMenu
This class implements accessibility support for the JPopupMenu class. |
private class |
JPopupMenu.ActionChangedListener
|
static class |
JPopupMenu.Separator
A popup menu-specific separator. |
Nested classes inherited from class javax.swing.JComponent |
JComponent.AccessibleJComponent, JComponent.ActionStandin, JComponent.IntVector, JComponent.KeyboardState |
Nested classes inherited from class java.awt.Container |
Container.AccessibleAWTContainer |
Nested classes inherited from class java.awt.Component |
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy |
Field Summary | |
private static Object |
classLock
|
private static boolean |
DEBUG
|
private static Object |
defaultLWPopupEnabledKey
Key used in AppContext to determine if light way popups are the default. |
private int |
desiredLocationX
|
private int |
desiredLocationY
|
(package private) Frame |
frame
|
(package private) Component |
invoker
|
private String |
label
|
private boolean |
lightWeightPopup
Used to indicate if lightweight popups should be used. |
private Insets |
margin
|
private boolean |
paintBorder
|
(package private) Popup |
popup
|
(package private) static boolean |
popupPostionFixDisabled
Bug#4425878-Property javax.swing.adjustPopupLocationToFit introduced |
private SingleSelectionModel |
selectionModel
|
private static boolean |
TRACE
|
private static String |
uiClassID
|
private static boolean |
VERBOSE
|
Fields inherited from class javax.swing.JComponent |
_bounds, accessibleContext, listenerList, paintingChild, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW |
Fields inherited from class java.awt.Container |
|
Fields inherited from class java.awt.Component |
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT |
Fields inherited from interface java.awt.image.ImageObserver |
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
Constructor Summary | |
JPopupMenu()
Constructs a JPopupMenu without an "invoker". |
|
JPopupMenu(String label)
Constructs a JPopupMenu with the specified title. |
Method Summary | |
JMenuItem |
add(Action a)
Appends a new menu item to the end of the menu which dispatches the specified Action object.
|
JMenuItem |
add(JMenuItem menuItem)
Appends the specified 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 |
addPopupMenuListener(PopupMenuListener l)
Adds a PopupMenu listener. |
void |
addSeparator()
Appends a new separator at the end of the menu. |
(package private) Point |
adjustPopupLocationToFitScreen(int xposition,
int yposition)
Returns an point which has been adjusted to take into account of the desktop bounds, taskbar and multi-monitor configuration. |
(package private) boolean |
alwaysOnTop()
Always returns true since popups, by definition, should always be on top of all other windows. |
protected java.beans.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 JPopupMenu .
|
protected void |
firePopupMenuCanceled()
Notifies PopupMenuListeners that this popup menu is
cancelled. |
protected void |
firePopupMenuWillBecomeInvisible()
Notifies PopupMenuListener s that this popup menu will
become invisible. |
protected void |
firePopupMenuWillBecomeVisible()
Notifies PopupMenuListener s that this popup menu will
become visible. |
javax.accessibility.AccessibleContext |
getAccessibleContext()
Gets the AccessibleContext associated with this JPopupMenu. |
Component |
getComponent()
Returns this JPopupMenu component. |
Component |
getComponentAtIndex(int i)
Deprecated. replaced by getComponent(int i) |
int |
getComponentIndex(Component c)
Returns the index of the specified component. |
static boolean |
getDefaultLightWeightPopupEnabled()
Gets the defaultLightWeightPopupEnabled property,
which by default is true . |
private static Frame |
getFrame(Component c)
|
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 containees. |
private Popup |
getPopup()
Returns a Popup instance from the
PopupMenuUI that has had show invoked on
it. |
PopupMenuListener[] |
getPopupMenuListeners()
Returns an array of all the PopupMenuListener s added
to this JMenuItem with addPopupMenuListener(). |
(package private) JPopupMenu |
getRootPopupMenu()
Returns the popup menu which is at the root of the menu system for this popup menu. |
SingleSelectionModel |
getSelectionModel()
Returns the model object that handles single selections. |
MenuElement[] |
getSubElements()
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 |
getUIClassID()
Returns the name of the L&F class that renders this component. |
void |
insert(Action a,
int index)
Inserts a menu item for the specified Action object at
a given position. |
void |
insert(Component component,
int index)
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. |
private boolean |
isPopupMenu()
Returns true if the popup menu is a standalone popup menu rather than the submenu of a JMenu . |
boolean |
isPopupTrigger(MouseEvent e)
Returns true if the MouseEvent is considered a popup trigger
by the JPopupMenu 's currently installed UI. |
(package private) boolean |
isSubPopupMenu(JPopupMenu popup)
Examines the list of menu items to determine whether popup is a popup menu. |
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 g)
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 evt)
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)
This method is required to conform to the MenuElement interface, but it not implemented. |
void |
processMouseEvent(MouseEvent event,
MenuElement[] path,
MenuSelectionManager manager)
This method is required to conform to the MenuElement interface, but it not implemented. |
private void |
readObject(ObjectInputStream s)
We use the ObjectInputStream "registerValidation"
callback to update the UI for the entire tree of components
after they've all been read in. |
void |
remove(int pos)
Removes the component at the specified index from this popup menu. |
void |
removePopupMenuListener(PopupMenuListener l)
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 |
setLabel(String label)
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 d)
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 |
setSelectionModel(SingleSelectionModel model)
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 |
show(Component invoker,
int x,
int y)
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. |
private void |
writeObject(ObjectOutputStream s)
Before writing a JComponent to an
ObjectOutputStream we temporarily uninstall its UI.
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
private static final String uiClassID
getUIClassID()
,
readObject(java.io.ObjectInputStream)
,
Constant Field Valuesprivate static final Object defaultLWPopupEnabledKey
static boolean popupPostionFixDisabled
transient Component invoker
transient Popup popup
transient Frame frame
private int desiredLocationX
private int desiredLocationY
private String label
private boolean paintBorder
private Insets margin
private boolean lightWeightPopup
private SingleSelectionModel selectionModel
private static final Object classLock
private static final boolean TRACE
private static final boolean VERBOSE
private static final boolean DEBUG
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 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
propertysetDefaultLightWeightPopupEnabled(boolean)
public PopupMenuUI getUI()
PopupMenuUI
object that renders this componentpublic void setUI(PopupMenuUI ui)
ui
- the new PopupMenuUI
L&F objectUIDefaults.getUI(javax.swing.JComponent)
public void updateUI()
updateUI
in class JComponent
JComponent.updateUI()
public String getUIClassID()
getUIClassID
in class JComponent
JComponent.getUIClassID()
,
UIDefaults.getUI(javax.swing.JComponent)
protected void processFocusEvent(FocusEvent evt)
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
.
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 eventFocusEvent
,
FocusListener
,
Component.addFocusListener(java.awt.event.FocusListener)
,
Component.enableEvents(long)
protected void processKeyEvent(KeyEvent evt)
processKeyEvent
in class JComponent
evt
- the key event to be processedpublic SingleSelectionModel getSelectionModel()
selectionModel
propertySingleSelectionModel
public void setSelectionModel(SingleSelectionModel model)
model
- the new SingleSelectionModel
SingleSelectionModel
public JMenuItem add(JMenuItem menuItem)
menuItem
- the JMenuItem
to add
JMenuItem
addedpublic JMenuItem add(String s)
s
- the string for the menu item to be addedpublic JMenuItem add(Action a)
Action
object.
As of JDK 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
.
a
- the Action
to add to the menu
Action
Point adjustPopupLocationToFitScreen(int xposition, int yposition)
This adustment may be cancelled by invoking the application with -Djavax.swing.adjustPopupLocationToFit=false
protected JMenuItem createActionComponent(Action a)
JMenuItem
for
Actions
added to the JPopupMenu
.
As of JDK 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
.
a
- the Action
for the menu item to be added
Action
protected java.beans.PropertyChangeListener createActionChangeListener(JMenuItem b)
PropertyChangeListener
which updates the control as changes to the Action
occur.
As of JDK 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
.
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 itemsContainer.add(java.awt.Component)
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 popupsisLightWeightPopupEnabled()
public boolean isLightWeightPopupEnabled()
lightWeightPopupEnabled
property.
lightWeightPopupEnabled
propertysetLightWeightPopupEnabled(boolean)
public String getLabel()
setLabel(java.lang.String)
public void setLabel(String label)
label
- a string specifying the label for the popup menusetLabel(java.lang.String)
public void addSeparator()
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 first
IllegalArgumentException
- if index
< 0Action
public void insert(Component component, int index)
component
- the Component
to insertindex
- specifies the position at which
to insert the component, where 0 is the first
IllegalArgumentException
- if index
< 0public void addPopupMenuListener(PopupMenuListener l)
PopupMenu
listener.
l
- the PopupMenuListener
to addpublic void removePopupMenuListener(PopupMenuListener l)
PopupMenu
listener.
l
- the PopupMenuListener
to removepublic PopupMenuListener[] getPopupMenuListeners()
PopupMenuListener
s added
to this JMenuItem with addPopupMenuListener().
PopupMenuListener
s added or an empty
array if no listeners have been addedprotected void firePopupMenuWillBecomeVisible()
PopupMenuListener
s that this popup menu will
become visible.
protected void firePopupMenuWillBecomeInvisible()
PopupMenuListener
s that this popup menu will
become invisible.
protected void firePopupMenuCanceled()
PopupMenuListeners
that this popup menu is
cancelled.
boolean alwaysOnTop()
alwaysOnTop
in class JComponent
public void pack()
public void setVisible(boolean b)
setVisible
in class JComponent
b
- true to make the popup visible, or false to
hide itprivate Popup getPopup()
Popup
instance from the
PopupMenuUI
that has had show
invoked on
it. If the current popup
is non-null,
this will invoke dispose
of it, and then
show
the new one.
This does NOT fire any events, it is up the caller to dispatch the necessary events.
public boolean isVisible()
isVisible
in class Component
true
if the component is visible,
false
otherwiseComponent.setVisible(boolean)
public void setLocation(int x, int y)
setLocation
in class Component
x
- the x coordinate of the popup's new positiony
- the y coordinate of the popup's new positionComponent.getLocation()
,
Component.setBounds(int, int, int, int)
private boolean isPopupMenu()
JMenu
.
public Component getInvoker()
Component
in which the popup menu is displayedpublic void setInvoker(Component invoker)
invoker
- the Component
in which the popup
menu is displayedpublic 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 displayedJPopupMenu getRootPopupMenu()
JPopupMenu
public Component getComponentAtIndex(int i)
getComponent(int i)
i
- the index of the component, where 0 is the first
Component
at that indexpublic int getComponentIndex(Component c)
c
- the Component
to find
public 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 boolean isBorderPainted()
setBorderPainted(boolean)
public void setBorderPainted(boolean b)
b
- if true, the border is painted.isBorderPainted()
protected void paintBorder(Graphics g)
borderPainted
property is true
.
paintBorder
in class JComponent
g
- the Graphics
objectJComponent.paint(java.awt.Graphics)
,
JComponent.setBorder(javax.swing.border.Border)
public Insets getMargin()
Insets
object containing the margin values.boolean isSubPopupMenu(JPopupMenu popup)
popup
is a popup menu.
popup
- a JPopupMenu
popup
private static Frame getFrame(Component c)
protected 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
.public javax.accessibility.AccessibleContext getAccessibleContext()
getAccessibleContext
in interface javax.accessibility.Accessible
getAccessibleContext
in class JComponent
private void writeObject(ObjectOutputStream s) throws IOException
JComponent
JComponent
to an
ObjectOutputStream
we temporarily uninstall its UI.
This is tricky to do because we want to uninstall
the UI before any of the JComponent
's children
(or its LayoutManager
etc.) are written,
and we don't want to restore the UI until the most derived
JComponent
subclass has been been stored.
IOException
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException
JComponent
ObjectInputStream
"registerValidation"
callback to update the UI for the entire tree of components
after they've all been read in.
IOException
ClassNotFoundException
public void processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)
MenuElement
interface, but it not implemented.
processMouseEvent
in interface MenuElement
MenuElement.processMouseEvent(MouseEvent, MenuElement[], MenuSelectionManager)
public void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)
MenuElement
interface, but it not implemented.
processKeyEvent
in interface MenuElement
MenuElement.processKeyEvent(KeyEvent, MenuElement[], 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 notMenuElement.menuSelectionChanged(boolean)
public 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.
getSubElements
in interface MenuElement
MenuElement
objectsMenuElement.getSubElements()
public Component getComponent()
JPopupMenu
component.
getComponent
in interface MenuElement
JPopupMenu
objectMenuElement.getComponent()
public boolean isPopupTrigger(MouseEvent e)
MouseEvent
is considered a popup trigger
by the JPopupMenu
's currently installed UI.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |