PopupMenupublic class PopupMenu extends Menu A class that implements a menu which can be dynamically popped up
at a specified position within a component.
As the inheritance hierarchy implies, a PopupMenu
can be used anywhere a Menu can be used.
However, if you use a PopupMenu like a Menu
(e.g., you add it to a MenuBar ), then you cannot
call show on that PopupMenu . |
Fields Summary |
---|
private static final String | base | static int | nameCounter | private static final long | serialVersionUID |
Constructors Summary |
---|
public PopupMenu()Creates a new popup menu with an empty name.
this("");
| public PopupMenu(String label)Creates a new popup menu with the specified name.
super(label);
|
Methods Summary |
---|
public void | addNotify()Creates the popup menu's peer.
The peer allows us to change the appearance of the popup menu without
changing any of the popup menu's functionality.
synchronized (getTreeLock()) {
// If our parent is not a Component, then this PopupMenu is
// really just a plain, old Menu.
if (parent != null && !(parent instanceof Component)) {
super.addNotify();
}
else {
if (peer == null)
peer = Toolkit.getDefaultToolkit().createPopupMenu(this);
int nitems = getItemCount();
for (int i = 0 ; i < nitems ; i++) {
MenuItem mi = getItem(i);
mi.parent = this;
mi.addNotify();
}
}
}
| java.lang.String | constructComponentName()Constructs a name for this MenuComponent .
Called by getName when the name is null .
synchronized (getClass()) {
return base + nameCounter++;
}
| public javax.accessibility.AccessibleContext | getAccessibleContext()Gets the AccessibleContext associated with this
PopupMenu .
if (accessibleContext == null) {
accessibleContext = new AccessibleAWTPopupMenu();
}
return accessibleContext;
| public void | show(java.awt.Component origin, int x, int y)Shows the popup menu at the x, y position relative to an origin
component.
The origin component must be contained within the component
hierarchy of the popup menu's parent. Both the origin and the parent
must be showing on the screen for this method to be valid.
If this PopupMenu is being used as a Menu
(i.e., it has a non-Component parent),
then you cannot call this method on the PopupMenu .
// Use localParent for thread safety.
MenuContainer localParent = parent;
if (localParent == null) {
throw new NullPointerException("parent is null");
}
if (!(localParent instanceof Component)) {
throw new IllegalArgumentException(
"PopupMenus with non-Component parents cannot be shown");
}
Component compParent = (Component)localParent;
if (compParent != origin &&
compParent instanceof Container &&
!((Container)compParent).isAncestorOf(origin)) {
throw new IllegalArgumentException(
"origin not in parent's hierarchy");
}
if (compParent.getPeer() == null || !compParent.isShowing()) {
throw new RuntimeException("parent not showing on screen");
}
if (peer == null) {
addNotify();
}
synchronized (getTreeLock()) {
if (peer != null) {
((PopupMenuPeer)peer).show(
new Event(origin, 0, Event.MOUSE_DOWN, x, y, 0, 0));
}
}
|
|