FileDocCategorySizeDatePackage
PopupMenu.javaAPI DocJava SE 6 API6844Tue Jun 10 00:25:18 BST 2008java.awt

PopupMenu

public 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.

version
1.34 04/07/06
author
Amy Fowler

Fields Summary
private static final String
base
static int
nameCounter
transient boolean
isTrayIconPopup
private static final long
serialVersionUID
Constructors Summary
public PopupMenu()
Creates a new popup menu with an empty name.

exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true.
see
java.awt.GraphicsEnvironment#isHeadless


                          
        
	this("");
    
public PopupMenu(String label)
Creates a new popup menu with the specified name.

param
label a non-null string specifying the popup menu's label
exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true.
see
java.awt.GraphicsEnvironment#isHeadless

	super(label);
    
Methods Summary
public voidaddNotify()
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.StringconstructComponentName()
Constructs a name for this MenuComponent. Called by getName when the name is null.

        synchronized (getClass()) {
	    return base + nameCounter++;
	}
    
public javax.accessibility.AccessibleContextgetAccessibleContext()
Gets the AccessibleContext associated with this PopupMenu.

return
the AccessibleContext of this PopupMenu
since
1.3

        if (accessibleContext == null) {
            accessibleContext = new AccessibleAWTPopupMenu();
        }
        return accessibleContext;
    
public java.awt.MenuContainergetParent()
{@inheritDoc}

        if (isTrayIconPopup) {
            return null;
        }
        return super.getParent();
    
public voidshow(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.

param
origin the component which defines the coordinate space
param
x the x coordinate position to popup the menu
param
y the y coordinate position to popup the menu
exception
NullPointerException if the parent is null
exception
IllegalArgumentException if this PopupMenu has a non-Component parent
exception
IllegalArgumentException if the origin is not in the parent's heirarchy
exception
RuntimeException if the parent is not showing on screen

        // 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;
        //Fixed 6278745: Incorrect exception throwing in PopupMenu.show() method  
        //Exception was not thrown if compParent was not equal to origin and
        //was not Container
        if (compParent != origin) {
            if (compParent instanceof Container) {
                if (!((Container)compParent).isAncestorOf(origin)) {
                    throw new IllegalArgumentException("origin not in parent's hierarchy");
                }
            } else {
                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));
	    }
	}