Methods Summary |
---|
public javax.swing.JMenuItem | add(javax.swing.JMenuItem menuItem)Appends a menu item to the end of this menu.
Returns the menu item added.
AccessibleContext ac = menuItem.getAccessibleContext();
ac.setAccessibleParent(this);
ensurePopupMenuCreated();
return popupMenu.add(menuItem);
|
public java.awt.Component | add(java.awt.Component c)Appends a component to the end of this menu.
Returns the component added.
if (c instanceof JComponent) {
AccessibleContext ac = ((JComponent) c).getAccessibleContext();
if (ac != null) {
ac.setAccessibleParent(this);
}
}
ensurePopupMenuCreated();
popupMenu.add(c);
return c;
|
public java.awt.Component | add(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.
if (c instanceof JComponent) {
AccessibleContext ac = ((JComponent) c).getAccessibleContext();
if (ac != null) {
ac.setAccessibleParent(this);
}
}
ensurePopupMenuCreated();
popupMenu.add(c, index);
return c;
|
public javax.swing.JMenuItem | add(java.lang.String s)Creates a new menu item with the specified text and appends
it to the end of this menu.
return add(new JMenuItem(s));
|
public javax.swing.JMenuItem | add(javax.swing.Action a)Creates a new menu item attached to the specified
Action object and appends it to the end of this menu.
As of 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 .
JMenuItem mi = createActionComponent(a);
mi.setAction(a);
add(mi);
return mi;
|
public void | addMenuListener(javax.swing.event.MenuListener l)Adds a listener for menu events.
listenerList.add(MenuListener.class, l);
|
public void | addSeparator()Appends a new separator to the end of the menu.
ensurePopupMenuCreated();
popupMenu.addSeparator();
|
public void | applyComponentOrientation(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}.
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;
}
}
|
protected void | configurePropertiesFromAction(javax.swing.Action a)Factory method which sets the ActionEvent
source's properties according to values from the
Action instance. The properties
which are set may differ for subclasses. By default,
the properties which get set are Text, Icon, Enabled,
ToolTipText, ActionCommand , and Mnemonic .
configurePropertiesFromAction(a, null);
|
protected java.beans.PropertyChangeListener | createActionChangeListener(javax.swing.JMenuItem b)Returns a properly configured PropertyChangeListener
which updates the control as changes to the Action occur.
As of 1.3, this is no longer the preferred method for adding
Action s 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 .
return new ActionChangedListener(b);
|
protected javax.swing.JMenuItem | createActionComponent(javax.swing.Action a)Factory method which creates the JMenuItem for
Action s added to the JMenu .
As of 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 .
JMenuItem mi = new JMenuItem((String)a.getValue(Action.NAME),
(Icon)a.getValue(Action.SMALL_ICON)){
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);
mi.setEnabled(a.isEnabled());
return mi;
|
private javax.swing.event.ChangeListener | createMenuChangeListener()
return new MenuChangeListener();
|
protected javax.swing.JMenu$WinListener | createWinListener(javax.swing.JPopupMenu p)Creates a window-closing listener for the popup.
return new WinListener(p);
|
public void | doClick(int pressTime)Programmatically performs a "click". This overrides the method
AbstractButton.doClick in order to make the menu pop up.
MenuElement me[] = buildMenuElementArray(this);
MenuSelectionManager.defaultManager().setSelectedPath(me);
|
private void | ensurePopupMenuCreated()
if (popupMenu == null) {
final JMenu thisMenu = this;
this.popupMenu = new JPopupMenu();
popupMenu.setInvoker(this);
popupListener = createWinListener(popupMenu);
}
|
protected void | fireMenuCanceled()Notifies all listeners that have registered interest for
notification on this event type. The event instance
is created lazily.
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 void | fireMenuDeselected()Notifies all listeners that have registered interest for
notification on this event type. The event instance
is created lazily.
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 void | fireMenuSelected()Notifies all listeners that have registered interest for
notification on this event type. The event instance
is created lazily.
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.AccessibleContext | getAccessibleContext()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.
if (accessibleContext == null) {
accessibleContext = new AccessibleJMenu();
}
return accessibleContext;
|
public java.awt.Component | getComponent()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.Point | getCustomMenuLocation()
return customMenuLocation;
|
public int | getDelay()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 delay;
|
public javax.swing.JMenuItem | getItem(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.
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 int | getItemCount()Returns the number of items on the menu, including separators.
This method is included for AWT compatibility.
return getMenuComponentCount();
|
public java.awt.Component | getMenuComponent(int n)Returns the component at position n .
if (popupMenu != null)
return popupMenu.getComponent(n);
return null;
|
public int | getMenuComponentCount()Returns 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 Component s of the menu's
subcomponents. Note that this returns all Component s
in the popup menu, including separators.
if (popupMenu != null)
return popupMenu.getComponents();
return new Component[0];
|
public javax.swing.event.MenuListener[] | getMenuListeners()Returns an array of all the MenuListener s added
to this JMenu with addMenuListener().
return (MenuListener[])listenerList.getListeners(MenuListener.class);
|
public javax.swing.JPopupMenu | getPopupMenu()Returns the popupmenu associated with this menu. If there is
no popupmenu, it will create one.
ensurePopupMenuCreated();
return popupMenu;
|
protected java.awt.Point | getPopupMenuOrigin()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.
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 MenuElement s 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
JSeparator s do not conform to the MenuElement
interface, this array will only contain JMenuItem s.
if(popupMenu == null)
return new MenuElement[0];
else {
MenuElement result[] = new MenuElement[1];
result[0] = popupMenu;
return result;
}
|
public java.lang.String | getUIClassID()Returns the name of the L&F class that renders this component.
return uiClassID;
|
void | initFocusability()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 void | insert(java.lang.String s, int pos)Inserts a new menu item with the specified text at a
given position.
if (pos < 0) {
throw new IllegalArgumentException("index less than zero.");
}
ensurePopupMenuCreated();
popupMenu.insert(new JMenuItem(s), pos);
|
public javax.swing.JMenuItem | insert(javax.swing.JMenuItem mi, int pos)Inserts the specified JMenuitem at a given position.
if (pos < 0) {
throw new IllegalArgumentException("index less than zero.");
}
AccessibleContext ac = mi.getAccessibleContext();
ac.setAccessibleParent(this);
ensurePopupMenuCreated();
popupMenu.insert(mi, pos);
return mi;
|
public javax.swing.JMenuItem | insert(javax.swing.Action a, int pos)Inserts a new menu item attached to the specified Action
object at a given position.
if (pos < 0) {
throw new IllegalArgumentException("index less than zero.");
}
ensurePopupMenuCreated();
JMenuItem mi = new JMenuItem((String)a.getValue(Action.NAME),
(Icon)a.getValue(Action.SMALL_ICON));
mi.setHorizontalTextPosition(JButton.TRAILING);
mi.setVerticalTextPosition(JButton.CENTER);
mi.setEnabled(a.isEnabled());
mi.setAction(a);
popupMenu.insert(mi, pos);
return mi;
|
public void | insertSeparator(int index)Inserts a separator at the specified position.
if (index < 0) {
throw new IllegalArgumentException("index less than zero.");
}
ensurePopupMenuCreated();
popupMenu.insert( new JPopupMenu.Separator(), index );
|
public boolean | isMenuComponent(java.awt.Component c)Returns true if the specified component exists in the
submenu hierarchy.
// 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 boolean | isPopupMenuVisible()Returns true if the menu's popup window is visible.
ensurePopupMenuCreated();
return popupMenu.isVisible();
|
public boolean | isSelected()Returns true if the menu is currently selected (highlighted).
return getModel().isSelected();
|
public boolean | isTearOff()Returns true if the menu can be torn off. This method is not
yet implemented.
throw new Error("boolean isTearOff() {} not yet implemented");
|
public boolean | isTopLevelMenu()Returns true if the menu is a 'top-level menu', that is, if it is
the direct child of a menubar.
if (getParent() instanceof JMenuBar)
return true;
return false;
|
public void | menuSelectionChanged(boolean isIncluded)Messaged when the menubar selection changes to activate or
deactivate this menu.
Overrides JMenuItem.menuSelectionChanged .
if (DEBUG) {
System.out.println("In JMenu.menuSelectionChanged to " + isIncluded);
}
setSelected(isIncluded);
|
protected java.lang.String | paramString()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 super.paramString();
|
protected void | processKeyEvent(java.awt.event.KeyEvent evt)Processes key stroke events such as mnemonics and accelerators.
MenuSelectionManager.defaultManager().processKeyEvent(evt);
if (evt.isConsumed())
return;
super.processKeyEvent(evt);
|
public void | remove(javax.swing.JMenuItem item)Removes the specified menu item from this menu. If there is no
popup menu, this method will have no effect.
if (popupMenu != null)
popupMenu.remove(item);
|
public void | remove(int pos)Removes the menu item at the specified index from this menu.
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 void | remove(java.awt.Component c)Removes the component c from this menu.
if (popupMenu != null)
popupMenu.remove(c);
|
public void | removeAll()Removes all menu items from this menu.
if (popupMenu != null)
popupMenu.removeAll();
|
public void | removeMenuListener(javax.swing.event.MenuListener l)Removes a listener for menu events.
listenerList.remove(MenuListener.class, l);
|
public void | setAccelerator(javax.swing.KeyStroke keyStroke)setAccelerator is not defined for JMenu .
Use setMnemonic instead.
throw new Error("setAccelerator() is not defined for JMenu. Use setMnemonic() instead.");
|
public void | setComponentOrientation(java.awt.ComponentOrientation o)
super.setComponentOrientation(o);
if ( popupMenu != null ) {
popupMenu.setComponentOrientation(o);
}
|
public void | setDelay(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.
if (d < 0)
throw new IllegalArgumentException("Delay must be a positive integer");
delay = d;
|
public void | setMenuLocation(int x, int y)Sets the location of the popup component.
customMenuLocation = new Point(x, y);
if (popupMenu != null)
popupMenu.setLocation(x, y);
|
public void | setModel(javax.swing.ButtonModel newModel)Sets the data model for the "menu button" -- the label
that the user clicks to open or close the menu.
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 void | setPopupMenuVisible(boolean b)Sets the visibility of the menu's popup. If the menu is
not enabled, this method will have no effect.
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 void | setSelected(boolean b)Sets the selection status of the menu.
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.Point | translateToPopupMenu(java.awt.Point p)
return translateToPopupMenu(p.x, p.y);
|
private java.awt.Point | translateToPopupMenu(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 void | updateUI()Resets the UI property with a value from the current look and feel.
setUI((MenuItemUI)UIManager.getUI(this));
if ( popupMenu != null )
{
popupMenu.setUI((PopupMenuUI)UIManager.getUI(popupMenu));
}
|
private void | writeObject(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);
}
}
|