FileDocCategorySizeDatePackage
JMenu.javaAPI DocJava SE 6 API53866Tue Jun 10 00:26:38 BST 2008javax.swing

JMenu

public class JMenu extends JMenuItem implements MenuElement, Accessible
An implementation of a menu -- a popup window containing JMenuItems that is displayed when the user selects an item on the JMenuBar. In addition to JMenuItems, a JMenu can also contain JSeparators.

In essence, a menu is a button with an associated JPopupMenu. When the "button" is pressed, the JPopupMenu appears. If the "button" is on the JMenuBar, the menu is a top-level window. If the "button" is another menu item, then the JPopupMenu is "pull-right" menu.

Menus can be configured, and to some degree controlled, by Actions. Using an Action with a menu has many benefits beyond directly configuring a menu. Refer to Swing Components Supporting Action for more details, and you can find more information in How to Use Actions, a section in The Java Tutorial.

For information and examples of using menus see How to Use Menus, a section 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}.

beaninfo
attribute: isContainer true description: A popup window containing menu items displayed in a menu bar.
version
1.181 08/08/06
author
Georges Saab
author
David Karlton
author
Arnaud Weber
see
JMenuItem
see
JSeparator
see
JMenuBar
see
JPopupMenu

Fields Summary
private static final String
uiClassID
private JPopupMenu
popupMenu
private ChangeListener
menuChangeListener
private MenuEvent
menuEvent
private static Hashtable
listenerRegistry
private int
delay
private Point
customMenuLocation
private static final boolean
TRACE
private static final boolean
VERBOSE
private static final boolean
DEBUG
protected WinListener
popupListener
The window-closing listener for the popup.
Constructors Summary
public JMenu()
Constructs a new JMenu with no text.

  // show bad params, misc.

                
      
        this("");
    
public JMenu(String s)
Constructs a new JMenu with the supplied string as its text.

param
s the text for the menu label

	super(s);
    
public JMenu(Action a)
Constructs a menu whose properties are taken from the Action supplied.

param
a an Action
since
1.3

        this();
	setAction(a);
    
public JMenu(String s, boolean b)
Constructs a new JMenu with the supplied string as its text and specified as a tear-off menu or not.

param
s the text for the menu label
param
b can the menu be torn off (not yet implemented)

        this(s);
    
Methods Summary
public javax.swing.JMenuItemadd(javax.swing.JMenuItem menuItem)
Appends a menu item to the end of this menu. Returns the menu item added.

param
menuItem the JMenuitem to be added
return
the JMenuItem added

        ensurePopupMenuCreated();
        return popupMenu.add(menuItem);
    
public java.awt.Componentadd(java.awt.Component c)
Appends a component to the end of this menu. Returns the component added.

param
c the Component to add
return
the Component added

        ensurePopupMenuCreated();
        popupMenu.add(c);
        return c;
    
public java.awt.Componentadd(java.awt.Component c, int index)
Adds the specified component to this container at the given position. If index equals -1, the component will be appended to the end.

param
c the Component to add
param
index the position at which to insert the component
return
the Component added
see
#remove
see
java.awt.Container#add(Component, int)

        ensurePopupMenuCreated();
        popupMenu.add(c, index);
        return c;
    
public javax.swing.JMenuItemadd(java.lang.String s)
Creates a new menu item with the specified text and appends it to the end of this menu.

param
s the string for the menu item to be added

        return add(new JMenuItem(s));
    
public javax.swing.JMenuItemadd(javax.swing.Action a)
Creates a new menu item attached to the specified Action object and appends it to the end of this menu.

param
a the Action for the menu item to be added
see
Action

	JMenuItem mi = createActionComponent(a);
        mi.setAction(a);
        add(mi);
        return mi;
    
public voidaddMenuListener(javax.swing.event.MenuListener l)
Adds a listener for menu events.

param
l the listener to be added

        listenerList.add(MenuListener.class, l);
    
public voidaddSeparator()
Appends a new separator to the end of the menu.

        ensurePopupMenuCreated();
        popupMenu.addSeparator();
    
public voidapplyComponentOrientation(java.awt.ComponentOrientation o)
Sets the ComponentOrientation property of this menu and all components contained within it. This includes all components returned by {@link #getMenuComponents getMenuComponents}.

param
o the new component orientation of this menu and the components contained within it.
exception
NullPointerException if orientation is null.
see
java.awt.Component#setComponentOrientation
see
java.awt.Component#getComponentOrientation
since
1.4

        super.applyComponentOrientation(o);
        
        if ( popupMenu != null ) {
            int ncomponents = getMenuComponentCount();
            for (int i = 0 ; i < ncomponents ; ++i) {
                getMenuComponent(i).applyComponentOrientation(o);
            }
            popupMenu.setComponentOrientation(o);
        }
    
private javax.swing.MenuElement[]buildMenuElementArray(javax.swing.JMenu leaf)

	Vector elements = new Vector();
	Component current = leaf.getPopupMenu();
	JPopupMenu pop;
	JMenu menu;
	JMenuBar bar;

	while (true) {
	    if (current instanceof JPopupMenu) {
		pop = (JPopupMenu) current;
		elements.insertElementAt(pop, 0);
		current = pop.getInvoker();
	    } else if (current instanceof JMenu) {
		menu = (JMenu) current;
		elements.insertElementAt(menu, 0);
		current = menu.getParent();
	    } else if (current instanceof JMenuBar) {
		bar = (JMenuBar) current;
		elements.insertElementAt(bar, 0);
		MenuElement me[] = new MenuElement[elements.size()];
		elements.copyInto(me);
		return me;
	    }
	}
    
voidconfigureAcceleratorFromAction(javax.swing.Action a)

    
protected java.beans.PropertyChangeListenercreateActionChangeListener(javax.swing.JMenuItem b)
Returns a properly configured PropertyChangeListener which updates the control as changes to the Action occur.

        return b.createActionPropertyChangeListener0(b.getAction());
    
protected javax.swing.JMenuItemcreateActionComponent(javax.swing.Action a)
Factory method which creates the JMenuItem for Actions added to the JMenu.

param
a the Action for the menu item to be added
return
the new menu item
see
Action
since
1.3

        JMenuItem mi = new JMenuItem() {
	    protected PropertyChangeListener createActionPropertyChangeListener(Action a) {
		PropertyChangeListener pcl = createActionChangeListener(this);
		if (pcl == null) {
		    pcl = super.createActionPropertyChangeListener(a);
		}
		return pcl;
	    }
	};
        mi.setHorizontalTextPosition(JButton.TRAILING);
        mi.setVerticalTextPosition(JButton.CENTER);
	return mi;
    
private javax.swing.event.ChangeListenercreateMenuChangeListener()

        return new MenuChangeListener();
    
protected javax.swing.JMenu$WinListenercreateWinListener(javax.swing.JPopupMenu p)
Creates a window-closing listener for the popup.

param
p the JPopupMenu
return
the new window-closing listener
see
WinListener

        return new WinListener(p);
    
public voiddoClick(int pressTime)
Programmatically performs a "click". This overrides the method AbstractButton.doClick in order to make the menu pop up.

param
pressTime indicates the number of milliseconds the button was pressed for

	MenuElement me[] = buildMenuElementArray(this);
	MenuSelectionManager.defaultManager().setSelectedPath(me);
    
private voidensurePopupMenuCreated()

        if (popupMenu == null) {            
            final JMenu thisMenu = this;
            this.popupMenu = new JPopupMenu();
            popupMenu.setInvoker(this);
            popupListener = createWinListener(popupMenu);
        }
    
protected voidfireMenuCanceled()
Notifies all listeners that have registered interest for notification on this event type. The event instance is created lazily.

exception
Error if there is a null listener
see
EventListenerList

	if (DEBUG) {
	    System.out.println("In JMenu.fireMenuCanceled");
	}
        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length-2; i>=0; i-=2) {
            if (listeners[i]==MenuListener.class) {
                if (listeners[i+1]== null) {
                    throw new Error(getText() +" has a NULL Listener!! " 
                                       + i);
                } else {
                    // Lazily create the event:
                    if (menuEvent == null)
                        menuEvent = new MenuEvent(this);
                    ((MenuListener)listeners[i+1]).menuCanceled(menuEvent);
                }              
            }
        }
    
protected voidfireMenuDeselected()
Notifies all listeners that have registered interest for notification on this event type. The event instance is created lazily.

exception
Error if there is a null listener
see
EventListenerList

	if (DEBUG) {
	    System.out.println("In JMenu.fireMenuDeselected");
	}
        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length-2; i>=0; i-=2) {
            if (listeners[i]==MenuListener.class) {
                if (listeners[i+1]== null) {
                    throw new Error(getText() +" has a NULL Listener!! " + i);
                } else {
                    // Lazily create the event:
                    if (menuEvent == null)
                        menuEvent = new MenuEvent(this);
                    ((MenuListener)listeners[i+1]).menuDeselected(menuEvent);
                }              
            }
        }
    
protected voidfireMenuSelected()
Notifies all listeners that have registered interest for notification on this event type. The event instance is created lazily.

exception
Error if there is a null listener
see
EventListenerList

	if (DEBUG) {
	    System.out.println("In JMenu.fireMenuSelected");
	}
        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length-2; i>=0; i-=2) {
            if (listeners[i]==MenuListener.class) {
                if (listeners[i+1]== null) {
		    throw new Error(getText() +" has a NULL Listener!! " + i);		    
                } else {
                    // Lazily create the event:
                    if (menuEvent == null)
                        menuEvent = new MenuEvent(this);
                    ((MenuListener)listeners[i+1]).menuSelected(menuEvent);
                }              
            }
        }
    
public javax.accessibility.AccessibleContextgetAccessibleContext()
Gets the AccessibleContext associated with this JMenu. For JMenus, the AccessibleContext takes the form of an AccessibleJMenu. A new AccessibleJMenu instance is created if necessary.

return
an AccessibleJMenu that serves as the AccessibleContext of this JMenu

        if (accessibleContext == null) {
            accessibleContext = new AccessibleJMenu();
        }
        return accessibleContext;
    
public java.awt.ComponentgetComponent()
Returns the java.awt.Component used to paint this MenuElement. The returned component is used to convert events and detect if an event is inside a menu component.

        return this;
    
private java.awt.PointgetCustomMenuLocation()

 	return customMenuLocation;
    
public intgetDelay()
Returns the suggested delay, in milliseconds, before submenus are popped up or down. Each look and feel (L&F) may determine its own policy for observing the delay property. In most cases, the delay is not observed for top level menus or while dragging. The default for delay is 0. This method is a property of the look and feel code and is used to manage the idiosyncracies of the various UI implementations.

return
the delay property

        return delay;
    
public javax.swing.JMenuItemgetItem(int pos)
Returns the JMenuItem at the specified position. If the component at pos is not a menu item, null is returned. This method is included for AWT compatibility.

param
pos an integer specifying the position
exception
IllegalArgumentException if the value of pos < 0
return
the menu item at the specified position; or null if the item as the specified position is not a menu item

        if (pos < 0) {
            throw new IllegalArgumentException("index less than zero.");
        }

        Component c = getMenuComponent(pos);
        if (c instanceof JMenuItem) {
            JMenuItem mi = (JMenuItem) c;
            return mi;
        }

        // 4173633
        return null;
    
public intgetItemCount()
Returns the number of items on the menu, including separators. This method is included for AWT compatibility.

return
an integer equal to the number of items on the menu
see
#getMenuComponentCount

        return getMenuComponentCount();
    
public java.awt.ComponentgetMenuComponent(int n)
Returns the component at position n.

param
n the position of the component to be returned
return
the component requested, or null if there is no popup menu

        if (popupMenu != null)
            return popupMenu.getComponent(n);
        
        return null;
    
public intgetMenuComponentCount()
Returns the number of components on the menu.

return
an integer containing the number of components on the menu

        int componentCount = 0;
        if (popupMenu != null)
            componentCount = popupMenu.getComponentCount();
        return componentCount;
    
public java.awt.Component[]getMenuComponents()
Returns an array of Components of the menu's subcomponents. Note that this returns all Components in the popup menu, including separators.

return
an array of Components or an empty array if there is no popup menu

        if (popupMenu != null)
            return popupMenu.getComponents();
        
        return new Component[0];
    
public javax.swing.event.MenuListener[]getMenuListeners()
Returns an array of all the MenuListeners added to this JMenu with addMenuListener().

return
all of the MenuListeners added or an empty array if no listeners have been added
since
1.4

        return (MenuListener[])listenerList.getListeners(MenuListener.class);
    
public javax.swing.JPopupMenugetPopupMenu()
Returns the popupmenu associated with this menu. If there is no popupmenu, it will create one.

        ensurePopupMenuCreated();
        return popupMenu;
    
protected java.awt.PointgetPopupMenuOrigin()
Computes the origin for the JMenu's popup menu. This method uses Look and Feel properties named Menu.menuPopupOffsetX, Menu.menuPopupOffsetY, Menu.submenuPopupOffsetX, and Menu.submenuPopupOffsetY to adjust the exact location of popup.

return
a Point in the coordinate space of the menu which should be used as the origin of the JMenu's popup menu
since
1.3

	int x = 0;
	int y = 0;
	JPopupMenu pm = getPopupMenu();
	// Figure out the sizes needed to caclulate the menu position
	Dimension s = getSize();
	Dimension pmSize = pm.getSize();
	// For the first time the menu is popped up, 
	// the size has not yet been initiated
	if (pmSize.width==0) {
	    pmSize = pm.getPreferredSize();
	}
	Point position = getLocationOnScreen();
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        GraphicsConfiguration gc = getGraphicsConfiguration();
        Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
        GraphicsEnvironment ge =
            GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gd = ge.getScreenDevices();
        for(int i = 0; i < gd.length; i++) {
            if(gd[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
                GraphicsConfiguration dgc =
                    gd[i].getDefaultConfiguration();
                if(dgc.getBounds().contains(position)) {
                    gc = dgc;
                    break;
                }
            }
        }


        if (gc != null) {
            screenBounds = gc.getBounds();
            // take screen insets (e.g. taskbar) into account
            Insets screenInsets = toolkit.getScreenInsets(gc);

            screenBounds.width -= 
                        Math.abs(screenInsets.left + screenInsets.right);
            screenBounds.height -= 
                        Math.abs(screenInsets.top + screenInsets.bottom);
            position.x -= Math.abs(screenInsets.left);
            position.y -= Math.abs(screenInsets.top);
        }
	
	Container parent = getParent();
	if (parent instanceof JPopupMenu) {
	    // We are a submenu (pull-right)
            int xOffset = UIManager.getInt("Menu.submenuPopupOffsetX");
            int yOffset = UIManager.getInt("Menu.submenuPopupOffsetY");

            if( SwingUtilities.isLeftToRight(this) ) {
                // First determine x:
                x = s.width + xOffset;   // Prefer placement to the right
                if (position.x + x + pmSize.width >= screenBounds.width 
                                                     + screenBounds.x && 
                    // popup doesn't fit - place it wherever there's more room
                    screenBounds.width - s.width < 2*(position.x
                                                    - screenBounds.x)) {

                    x = 0 - xOffset - pmSize.width;
                }
            } else {
                // First determine x:
                x = 0 - xOffset - pmSize.width; // Prefer placement to the left
                if (position.x + x < screenBounds.x &&
                    // popup doesn't fit - place it wherever there's more room
                    screenBounds.width - s.width > 2*(position.x -
                                                    screenBounds.x)) {

                    x = s.width + xOffset;
                }
            }
            // Then the y:
            y = yOffset;                     // Prefer dropping down
            if (position.y + y + pmSize.height >= screenBounds.height 
                                                  + screenBounds.y &&
                // popup doesn't fit - place it wherever there's more room
                screenBounds.height - s.height < 2*(position.y 
                                                  - screenBounds.y)) {

                y = s.height - yOffset - pmSize.height;
            }
	} else {
	    // We are a toplevel menu (pull-down)
            int xOffset = UIManager.getInt("Menu.menuPopupOffsetX");
            int yOffset = UIManager.getInt("Menu.menuPopupOffsetY");

            if( SwingUtilities.isLeftToRight(this) ) {
                // First determine the x:
                x = xOffset;                   // Extend to the right
                if (position.x + x + pmSize.width >= screenBounds.width 
                                                     + screenBounds.x &&
                    // popup doesn't fit - place it wherever there's more room
                    screenBounds.width - s.width < 2*(position.x 
                                                    - screenBounds.x)) {

                    x = s.width - xOffset - pmSize.width;
                }
            } else {
                // First determine the x:
                x = s.width - xOffset - pmSize.width; // Extend to the left
                if (position.x + x < screenBounds.x &&
                    // popup doesn't fit - place it wherever there's more room
                    screenBounds.width - s.width > 2*(position.x
                                                    - screenBounds.x)) {

                    x = xOffset;
                }
            }
	    // Then the y:
            y = s.height + yOffset;    // Prefer dropping down
	    if (position.y + y + pmSize.height >= screenBounds.height &&
                // popup doesn't fit - place it wherever there's more room
                screenBounds.height - s.height < 2*(position.y 
                                                  - screenBounds.y)) {

		y = 0 - yOffset - pmSize.height;   // Otherwise drop 'up'
	    }
	}
	return new Point(x,y);
    
public javax.swing.MenuElement[]getSubElements()
Returns an array of MenuElements containing the submenu for this menu component. If popup menu is null returns an empty array. This method is required to conform to the MenuElement interface. Note that since JSeparators do not conform to the MenuElement interface, this array will only contain JMenuItems.

return
an array of MenuElement objects

        if(popupMenu == null)
            return new MenuElement[0];
        else {
            MenuElement result[] = new MenuElement[1];
            result[0] = popupMenu;
            return result;
        }
    
public java.lang.StringgetUIClassID()
Returns the name of the L&F class that renders this component.

return
the string "MenuUI"
see
JComponent#getUIClassID
see
UIDefaults#getUI

        return uiClassID;
    
voidinitFocusability()
Overriden to do nothing. We want JMenu to be focusable, but JMenuItem doesn't want to be, thus we override this do nothing. We don't invoke setFocusable(true) after super's constructor has completed as this has the side effect that JMenu will be considered traversable via the keyboard, which we don't want. Making a Component traversable by the keyboard after invoking setFocusable(true) is OK, as setFocusable is new API and is speced as such, but internally we don't want to use it like this else we change the keyboard traversability.

    
public voidinsert(java.lang.String s, int pos)
Inserts a new menu item with the specified text at a given position.

param
s the text for the menu item to add
param
pos an integer specifying the position at which to add the new menu item
exception
IllegalArgumentException when the value of pos < 0

        if (pos < 0) {
            throw new IllegalArgumentException("index less than zero.");
        }

        ensurePopupMenuCreated();
        popupMenu.insert(new JMenuItem(s), pos);
    
public javax.swing.JMenuIteminsert(javax.swing.JMenuItem mi, int pos)
Inserts the specified JMenuitem at a given position.

param
mi the JMenuitem to add
param
pos an integer specifying the position at which to add the new JMenuitem
return
the new menu item
exception
IllegalArgumentException if the value of pos < 0

        if (pos < 0) {
            throw new IllegalArgumentException("index less than zero.");
        }
        ensurePopupMenuCreated();
        popupMenu.insert(mi, pos);
        return mi;
    
public javax.swing.JMenuIteminsert(javax.swing.Action a, int pos)
Inserts a new menu item attached to the specified Action object at a given position.

param
a the Action object for the menu item to add
param
pos an integer specifying the position at which to add the new menu item
exception
IllegalArgumentException if the value of pos < 0

        if (pos < 0) {
            throw new IllegalArgumentException("index less than zero.");
        }

        ensurePopupMenuCreated();
        JMenuItem mi = new JMenuItem(a);
        mi.setHorizontalTextPosition(JButton.TRAILING);
        mi.setVerticalTextPosition(JButton.CENTER);
        popupMenu.insert(mi, pos);
        return mi;
    
public voidinsertSeparator(int index)
Inserts a separator at the specified position.

param
index an integer specifying the position at which to insert the menu separator
exception
IllegalArgumentException if the value of index < 0

        if (index < 0) {
            throw new IllegalArgumentException("index less than zero.");
        }

        ensurePopupMenuCreated();
        popupMenu.insert( new JPopupMenu.Separator(), index );
    
public booleanisMenuComponent(java.awt.Component c)
Returns true if the specified component exists in the submenu hierarchy.

param
c the Component to be tested
return
true if the Component exists, false otherwise

        // Are we in the MenuItem part of the menu
        if (c == this)
            return true;
        // Are we in the PopupMenu?
        if (c instanceof JPopupMenu) {
            JPopupMenu comp = (JPopupMenu) c;
            if (comp == this.getPopupMenu())
                return true;
        }
        // Are we in a Component on the PopupMenu
        int ncomponents = this.getMenuComponentCount();
        Component[] component = this.getMenuComponents();
        for (int i = 0 ; i < ncomponents ; i++) {
            Component comp = component[i];
            // Are we in the current component?
            if (comp == c)
                return true;
            // Hmmm, what about Non-menu containers?

            // Recursive call for the Menu case
            if (comp instanceof JMenu) {
                JMenu subMenu = (JMenu) comp;
                if (subMenu.isMenuComponent(c))
                    return true;
            }
        }
        return false;
    
public booleanisPopupMenuVisible()
Returns true if the menu's popup window is visible.

return
true if the menu is visible, else false

        ensurePopupMenuCreated();
        return popupMenu.isVisible();
    
public booleanisSelected()
Returns true if the menu is currently selected (highlighted).

return
true if the menu is selected, else false

        return getModel().isSelected();
    
public booleanisTearOff()
Returns true if the menu can be torn off. This method is not yet implemented.

return
true if the menu can be torn off, else false
exception
Error if invoked -- this method is not yet implemented

        throw new Error("boolean isTearOff() {} not yet implemented");
    
public booleanisTopLevelMenu()
Returns true if the menu is a 'top-level menu', that is, if it is the direct child of a menubar.

return
true if the menu is activated from the menu bar; false if the menu is activated from a menu item on another menu

        if (getParent() instanceof JMenuBar)
            return true;
        
        return false;
    
public voidmenuSelectionChanged(boolean isIncluded)
Messaged when the menubar selection changes to activate or deactivate this menu. Overrides JMenuItem.menuSelectionChanged.

param
isIncluded true if this menu is active, false if it is not

	if (DEBUG) {
	    System.out.println("In JMenu.menuSelectionChanged to " + isIncluded);
	}
        setSelected(isIncluded);
    
protected java.lang.StringparamString()
Returns a string representation of this JMenu. 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.

return
a string representation of this JMenu.

	return super.paramString();
    
protected voidprocessKeyEvent(java.awt.event.KeyEvent evt)
Processes key stroke events such as mnemonics and accelerators.

param
evt the key event to be processed

	MenuSelectionManager.defaultManager().processKeyEvent(evt);
	if (evt.isConsumed())
	    return;

	super.processKeyEvent(evt);
    
public voidremove(javax.swing.JMenuItem item)
Removes the specified menu item from this menu. If there is no popup menu, this method will have no effect.

param
item the JMenuItem to be removed from the menu

        if (popupMenu != null)
	    popupMenu.remove(item);
    
public voidremove(int pos)
Removes the menu item at the specified index from this menu.

param
pos the position of the item to be removed
exception
IllegalArgumentException if the value of pos < 0, or if pos is greater than the number of menu items

        if (pos < 0) {
            throw new IllegalArgumentException("index less than zero.");
        }
        if (pos > getItemCount()) {
            throw new IllegalArgumentException("index greater than the number of items.");
        }
        if (popupMenu != null)
	    popupMenu.remove(pos);
    
public voidremove(java.awt.Component c)
Removes the component c from this menu.

param
c the component to be removed

        if (popupMenu != null)
	    popupMenu.remove(c);
    
public voidremoveAll()
Removes all menu items from this menu.

        if (popupMenu != null)
	    popupMenu.removeAll();
    
public voidremoveMenuListener(javax.swing.event.MenuListener l)
Removes a listener for menu events.

param
l the listener to be removed

        listenerList.remove(MenuListener.class, l);
    
public voidsetAccelerator(javax.swing.KeyStroke keyStroke)
setAccelerator is not defined for JMenu. Use setMnemonic instead.

param
keyStroke the keystroke combination which will invoke the JMenuItem's actionlisteners without navigating the menu hierarchy
exception
Error if invoked -- this method is not defined for JMenu. Use setMnemonic instead
beaninfo
description: The keystroke combination which will invoke the JMenuItem's actionlisteners without navigating the menu hierarchy hidden: true

        throw new Error("setAccelerator() is not defined for JMenu.  Use setMnemonic() instead.");
    
public voidsetComponentOrientation(java.awt.ComponentOrientation o)

        super.setComponentOrientation(o);
        if ( popupMenu != null ) {
            popupMenu.setComponentOrientation(o);
        }
    
public voidsetDelay(int d)
Sets the suggested delay before the menu's PopupMenu is popped up or down. Each look and feel (L&F) may determine it's own policy for observing the delay property. In most cases, the delay is not observed for top level menus or while dragging. This method is a property of the look and feel code and is used to manage the idiosyncracies of the various UI implementations.

param
d the number of milliseconds to delay
exception
IllegalArgumentException if d is less than 0
beaninfo
description: The delay between menu selection and making the popup menu visible expert: true

        if (d < 0)
            throw new IllegalArgumentException("Delay must be a positive integer");

        delay = d;
    
public voidsetMenuLocation(int x, int y)
Sets the location of the popup component.

param
x the x coordinate of the popup's new position
param
y the y coordinate of the popup's new position

 	customMenuLocation = new Point(x, y);
        if (popupMenu != null)
	    popupMenu.setLocation(x, y);
    
public voidsetModel(javax.swing.ButtonModel newModel)
Sets the data model for the "menu button" -- the label that the user clicks to open or close the menu.

param
newModel the ButtonModel
see
#getModel
beaninfo
description: The menu's model bound: true expert: true hidden: true

        ButtonModel oldModel = getModel();

        super.setModel(newModel);

        if (oldModel != null && menuChangeListener != null) {
            oldModel.removeChangeListener(menuChangeListener);
            menuChangeListener = null;
        }
        
        model = newModel;
        
        if (newModel != null) {
            menuChangeListener = createMenuChangeListener();
            newModel.addChangeListener(menuChangeListener);
        }
    
public voidsetPopupMenuVisible(boolean b)
Sets the visibility of the menu's popup. If the menu is not enabled, this method will have no effect.

param
b a boolean value -- true to make the menu visible, false to hide it
beaninfo
description: The popup menu's visibility expert: true hidden: true

	if (DEBUG) {
	    System.out.println("in JMenu.setPopupMenuVisible " + b);
	    // Thread.dumpStack();
	}

	boolean isVisible = isPopupMenuVisible();
        if (b != isVisible && (isEnabled() || !b)) {
            ensurePopupMenuCreated();
            if ((b==true) && isShowing()) {
                // Set location of popupMenu (pulldown or pullright)
 		Point p = getCustomMenuLocation();
 		if (p == null) {
 		    p = getPopupMenuOrigin();
 		}
		getPopupMenu().show(this, p.x, p.y);
            } else {
                getPopupMenu().setVisible(false);
            }
        }
    
public voidsetSelected(boolean b)
Sets the selection status of the menu.

param
b true to select (highlight) the menu; false to de-select the menu
beaninfo
description: When the menu is selected, its popup child is shown. expert: true hidden: true

        ButtonModel model = getModel();
        boolean oldValue = model.isSelected();

        // TIGER - 4840653
        // Removed code which fired an AccessibleState.SELECTED
        // PropertyChangeEvent since this resulted in two
        // identical events being fired since
        // AbstractButton.fireItemStateChanged also fires the
        // same event. This caused screen readers to speak the
        // name of the item twice.

        if (b != model.isSelected()) {
            getModel().setSelected(b);
        }
    
private java.awt.PointtranslateToPopupMenu(java.awt.Point p)

        return translateToPopupMenu(p.x, p.y);
    
private java.awt.PointtranslateToPopupMenu(int x, int y)

            int newX;
            int newY;

            if (getParent() instanceof JPopupMenu) {
                newX = x - getSize().width;
                newY = y;
            } else {
                newX = x;
                newY = y - getSize().height;
            }

            return new Point(newX, newY);
        
public voidupdateUI()
Resets the UI property with a value from the current look and feel.

see
JComponent#updateUI

        setUI((MenuItemUI)UIManager.getUI(this));

        if ( popupMenu != null )
          {
            popupMenu.setUI((PopupMenuUI)UIManager.getUI(popupMenu));
          }

    
private voidwriteObject(java.io.ObjectOutputStream s)
See readObject and writeObject in JComponent for more information about serialization in Swing.

        s.defaultWriteObject();
        if (getUIClassID().equals(uiClassID)) {
            byte count = JComponent.getWriteObjCounter(this);
            JComponent.setWriteObjCounter(this, --count);
            if (count == 0 && ui != null) {
                ui.installUI(this);
            }
        }