| Method from javax.swing.JMenu Detail: |
public JMenuItem add(JMenuItem menuItem) {
ensurePopupMenuCreated();
return popupMenu.add(menuItem);
}
Appends a menu item to the end of this menu.
Returns the menu item added. |
public Component add(Component c) {
ensurePopupMenuCreated();
popupMenu.add(c);
return c;
}
Appends a component to the end of this menu.
Returns the component added. |
public JMenuItem add(String s) {
return add(new JMenuItem(s));
}
Creates a new menu item with the specified text and appends
it to the end of this menu. |
public JMenuItem add(Action a) {
JMenuItem mi = createActionComponent(a);
mi.setAction(a);
add(mi);
return mi;
}
Creates a new menu item attached to the specified
Action object and appends it to the end of this menu. |
public Component add(Component c,
int index) {
ensurePopupMenuCreated();
popupMenu.add(c, index);
return c;
}
Adds the specified component to this container at the given
position. If index equals -1, the component will
be appended to the end. |
public void addMenuListener(MenuListener l) {
listenerList.add(MenuListener.class, l);
}
Adds a listener for menu events. |
public void addSeparator() {
ensurePopupMenuCreated();
popupMenu.addSeparator();
}
Appends a new separator to the end of the menu. |
public void applyComponentOrientation(ComponentOrientation o) {
super.applyComponentOrientation(o);
if ( popupMenu != null ) {
int ncomponents = getMenuComponentCount();
for (int i = 0 ; i < ncomponents ; ++i) {
getMenuComponent(i).applyComponentOrientation(o);
}
popupMenu.setComponentOrientation(o);
}
}
Sets the ComponentOrientation property of this menu
and all components contained within it. This includes all
components returned by getMenuComponents . |
void configureAcceleratorFromAction(Action a) {
}
|
protected PropertyChangeListener createActionChangeListener(JMenuItem b) {
return b.createActionPropertyChangeListener0(b.getAction());
}
Returns a properly configured PropertyChangeListener
which updates the control as changes to the Action occur. |
protected JMenuItem createActionComponent(Action a) {
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;
}
Factory method which creates the JMenuItem for
Actions added to the JMenu. |
protected JMenu.WinListener createWinListener(JPopupMenu p) {
return new WinListener(p);
}
Creates a window-closing listener for the popup. |
public void doClick(int pressTime) {
MenuElement me[] = buildMenuElementArray(this);
MenuSelectionManager.defaultManager().setSelectedPath(me);
}
Programmatically performs a "click". This overrides the method
AbstractButton.doClick in order to make the menu pop up. |
protected void fireMenuCanceled() {
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);
}
}
}
}
Notifies all listeners that have registered interest for
notification on this event type. The event instance
is created lazily. |
protected void fireMenuDeselected() {
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);
}
}
}
}
Notifies all listeners that have registered interest for
notification on this event type. The event instance
is created lazily. |
protected void fireMenuSelected() {
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);
}
}
}
}
Notifies all listeners that have registered interest for
notification on this event type. The event instance
is created lazily. |
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleJMenu();
}
return accessibleContext;
}
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. |
public Component getComponent() {
return this;
}
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. |
public int getDelay() {
return delay;
}
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. |
public JMenuItem getItem(int pos) {
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;
}
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. |
public int getItemCount() {
return getMenuComponentCount();
}
Returns the number of items on the menu, including separators.
This method is included for AWT compatibility. |
public Component getMenuComponent(int n) {
if (popupMenu != null)
return popupMenu.getComponent(n);
return null;
}
Returns the component at position n. |
public int getMenuComponentCount() {
int componentCount = 0;
if (popupMenu != null)
componentCount = popupMenu.getComponentCount();
return componentCount;
}
Returns the number of components on the menu. |
public Component[] getMenuComponents() {
if (popupMenu != null)
return popupMenu.getComponents();
return new Component[0];
}
Returns an array of Components of the menu's
subcomponents. Note that this returns all Components
in the popup menu, including separators. |
public MenuListener[] getMenuListeners() {
return (MenuListener[])listenerList.getListeners(MenuListener.class);
}
Returns an array of all the MenuListeners added
to this JMenu with addMenuListener(). |
public JPopupMenu getPopupMenu() {
ensurePopupMenuCreated();
return popupMenu;
}
Returns the popupmenu associated with this menu. If there is
no popupmenu, it will create one. |
protected Point getPopupMenuOrigin() {
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);
}
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. |
public MenuElement[] getSubElements() {
if(popupMenu == null)
return new MenuElement[0];
else {
MenuElement result[] = new MenuElement[1];
result[0] = popupMenu;
return result;
}
}
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. |
public String getUIClassID() {
return uiClassID;
}
Returns the name of the L&F class that renders this component. |
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(String s,
int pos) {
if (pos < 0) {
throw new IllegalArgumentException("index less than zero.");
}
ensurePopupMenuCreated();
popupMenu.insert(new JMenuItem(s), pos);
}
Inserts a new menu item with the specified text at a
given position. |
public JMenuItem insert(JMenuItem mi,
int pos) {
if (pos < 0) {
throw new IllegalArgumentException("index less than zero.");
}
ensurePopupMenuCreated();
popupMenu.insert(mi, pos);
return mi;
}
Inserts the specified JMenuitem at a given position. |
public JMenuItem insert(Action a,
int pos) {
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;
}
Inserts a new menu item attached to the specified Action
object at a given position. |
public void insertSeparator(int index) {
if (index < 0) {
throw new IllegalArgumentException("index less than zero.");
}
ensurePopupMenuCreated();
popupMenu.insert( new JPopupMenu.Separator(), index );
}
Inserts a separator at the specified position. |
public boolean isMenuComponent(Component c) {
// 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;
}
Returns true if the specified component exists in the
submenu hierarchy. |
public boolean isPopupMenuVisible() {
ensurePopupMenuCreated();
return popupMenu.isVisible();
}
Returns true if the menu's popup window is visible. |
public boolean isSelected() {
return getModel().isSelected();
}
Returns true if the menu is currently selected (highlighted). |
public boolean isTearOff() {
throw new Error("boolean isTearOff() {} not yet implemented");
}
Returns true if the menu can be torn off. This method is not
yet implemented. |
public boolean isTopLevelMenu() {
if (getParent() instanceof JMenuBar)
return true;
return false;
}
Returns true if the menu is a 'top-level menu', that is, if it is
the direct child of a menubar. |
public void menuSelectionChanged(boolean isIncluded) {
if (DEBUG) {
System.out.println("In JMenu.menuSelectionChanged to " + isIncluded);
}
setSelected(isIncluded);
}
Messaged when the menubar selection changes to activate or
deactivate this menu.
Overrides JMenuItem.menuSelectionChanged. |
protected String paramString() {
return super.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. |
protected void processKeyEvent(KeyEvent evt) {
MenuSelectionManager.defaultManager().processKeyEvent(evt);
if (evt.isConsumed())
return;
super.processKeyEvent(evt);
}
Processes key stroke events such as mnemonics and accelerators. |
public void remove(JMenuItem item) {
if (popupMenu != null)
popupMenu.remove(item);
}
Removes the specified menu item from this menu. If there is no
popup menu, this method will have no effect. |
public void remove(int pos) {
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);
}
Removes the menu item at the specified index from this menu. |
public void remove(Component c) {
if (popupMenu != null)
popupMenu.remove(c);
}
Removes the component c from this menu. |
public void removeAll() {
if (popupMenu != null)
popupMenu.removeAll();
}
Removes all menu items from this menu. |
public void removeMenuListener(MenuListener l) {
listenerList.remove(MenuListener.class, l);
}
Removes a listener for menu events. |
public void setAccelerator(KeyStroke keyStroke) {
throw new Error("setAccelerator() is not defined for JMenu. Use setMnemonic() instead.");
}
setAccelerator is not defined for JMenu.
Use setMnemonic instead.
|
public void setComponentOrientation(ComponentOrientation o) {
super.setComponentOrientation(o);
if ( popupMenu != null ) {
popupMenu.setComponentOrientation(o);
}
}
|
public void setDelay(int d) {
if (d < 0)
throw new IllegalArgumentException("Delay must be a positive integer");
delay = 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. |
public void setMenuLocation(int x,
int y) {
customMenuLocation = new Point(x, y);
if (popupMenu != null)
popupMenu.setLocation(x, y);
}
Sets the location of the popup component. |
public void setModel(ButtonModel newModel) {
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);
}
}
Sets the data model for the "menu button" -- the label
that the user clicks to open or close the menu. |
public void setPopupMenuVisible(boolean b) {
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);
}
}
}
Sets the visibility of the menu's popup. If the menu is
not enabled, this method will have no effect. |
public void setSelected(boolean b) {
ButtonModel model = getModel();
boolean oldValue = model.isSelected();
// TIGER - 4840653
// Removed code which fired an AccessibleState.SELECTED
// PropertyChangeEvent since this resulted in two
// identical event
|