An implementation of an item in a menu. A menu item is essentially a button
sitting in a list. When the user selects the "button", the action
associated with the menu item is performed. A
performs exactly that function.
| Method from javax.swing.JMenuItem Detail: |
protected void actionPropertyChanged(Action action,
String propertyName) {
if (propertyName == Action.ACCELERATOR_KEY) {
configureAcceleratorFromAction(action);
}
else {
super.actionPropertyChanged(action, propertyName);
}
}
|
public void addMenuDragMouseListener(MenuDragMouseListener l) {
listenerList.add(MenuDragMouseListener.class, l);
}
Adds a MenuDragMouseListener to the menu item. |
public void addMenuKeyListener(MenuKeyListener l) {
listenerList.add(MenuKeyListener.class, l);
}
Adds a MenuKeyListener to the menu item. |
boolean alwaysOnTop() {
// Fix for bug #4482165
if (SwingUtilities.getAncestorOfClass(JInternalFrame.class, this) !=
null) {
return false;
}
return true;
}
Returns true since Menus, by definition,
should always be on top of all other windows. If the menu is
in an internal frame false is returned due to the rollover effect
for windows laf where the menu is not always on top. |
void configureAcceleratorFromAction(Action a) {
KeyStroke ks = (a==null) ? null :
(KeyStroke)a.getValue(Action.ACCELERATOR_KEY);
setAccelerator(ks);
}
|
protected void configurePropertiesFromAction(Action a) {
super.configurePropertiesFromAction(a);
configureAcceleratorFromAction(a);
}
|
protected void fireMenuDragMouseDragged(MenuDragMouseEvent event) {
// 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]==MenuDragMouseListener.class) {
// Lazily create the event:
((MenuDragMouseListener)listeners[i+1]).menuDragMouseDragged(event);
}
}
}
Notifies all listeners that have registered interest for
notification on this event type. |
protected void fireMenuDragMouseEntered(MenuDragMouseEvent event) {
// 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]==MenuDragMouseListener.class) {
// Lazily create the event:
((MenuDragMouseListener)listeners[i+1]).menuDragMouseEntered(event);
}
}
}
Notifies all listeners that have registered interest for
notification on this event type. |
protected void fireMenuDragMouseExited(MenuDragMouseEvent event) {
// 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]==MenuDragMouseListener.class) {
// Lazily create the event:
((MenuDragMouseListener)listeners[i+1]).menuDragMouseExited(event);
}
}
}
Notifies all listeners that have registered interest for
notification on this event type. |
protected void fireMenuDragMouseReleased(MenuDragMouseEvent event) {
// 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]==MenuDragMouseListener.class) {
// Lazily create the event:
((MenuDragMouseListener)listeners[i+1]).menuDragMouseReleased(event);
}
}
}
Notifies all listeners that have registered interest for
notification on this event type. |
protected void fireMenuKeyPressed(MenuKeyEvent event) {
if (DEBUG) {
System.out.println("in JMenuItem.fireMenuKeyPressed for " + getText()+
" " + KeyStroke.getKeyStrokeForEvent(event));
}
// 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]==MenuKeyListener.class) {
// Lazily create the event:
((MenuKeyListener)listeners[i+1]).menuKeyPressed(event);
}
}
}
Notifies all listeners that have registered interest for
notification on this event type. |
protected void fireMenuKeyReleased(MenuKeyEvent event) {
if (DEBUG) {
System.out.println("in JMenuItem.fireMenuKeyReleased for " + getText()+
" " + KeyStroke.getKeyStrokeForEvent(event));
}
// 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]==MenuKeyListener.class) {
// Lazily create the event:
((MenuKeyListener)listeners[i+1]).menuKeyReleased(event);
}
}
}
Notifies all listeners that have registered interest for
notification on this event type. |
protected void fireMenuKeyTyped(MenuKeyEvent event) {
if (DEBUG) {
System.out.println("in JMenuItem.fireMenuKeyTyped for " + getText()+
" " + KeyStroke.getKeyStrokeForEvent(event));
}
// 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]==MenuKeyListener.class) {
// Lazily create the event:
((MenuKeyListener)listeners[i+1]).menuKeyTyped(event);
}
}
}
Notifies all listeners that have registered interest for
notification on this event type. |
public KeyStroke getAccelerator() {
return this.accelerator;
}
Returns the KeyStroke which serves as an accelerator
for the menu item. |
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleJMenuItem();
}
return accessibleContext;
}
Returns the AccessibleContext associated with this
JMenuItem. For JMenuItems,
the AccessibleContext takes the form of an
AccessibleJMenuItem.
A new AccessibleJMenuItme instance is created if necessary. |
public Component getComponent() {
return this;
}
Returns the java.awt.Component used to paint
this object. The returned component will be used to convert
events and detect if an event is inside a menu component. |
public MenuDragMouseListener[] getMenuDragMouseListeners() {
return (MenuDragMouseListener[])listenerList.getListeners(
MenuDragMouseListener.class);
}
Returns an array of all the MenuDragMouseListeners added
to this JMenuItem with addMenuDragMouseListener(). |
public MenuKeyListener[] getMenuKeyListeners() {
return (MenuKeyListener[])listenerList.getListeners(
MenuKeyListener.class);
}
Returns an array of all the MenuKeyListeners added
to this JMenuItem with addMenuKeyListener(). |
public MenuElement[] getSubElements() {
return new MenuElement[0];
}
This method returns an array containing the sub-menu
components for this menu component. |
public String getUIClassID() {
return uiClassID;
}
Returns the suffix used to construct the name of the L&F class used to
render this component. |
protected void init(String text,
Icon icon) {
if(text != null) {
setText(text);
}
if(icon != null) {
setIcon(icon);
}
// Listen for Focus events
addFocusListener(new MenuItemFocusListener());
setUIProperty("borderPainted", Boolean.FALSE);
setFocusPainted(false);
setHorizontalTextPosition(JButton.TRAILING);
setHorizontalAlignment(JButton.LEADING);
updateUI();
}
Initializes the menu item with the specified text and icon. |
void initFocusability() {
setFocusable(false);
}
Inititalizes the focusability of the the JMenuItem.
JMenuItem's are focusable, but subclasses may
want to be, this provides them the opportunity to override this
and invoke something else, or nothing at all. Refer to
javax.swing.JMenu#initFocusability for the motivation of
this. |
public boolean isArmed() {
ButtonModel model = (ButtonModel) getModel();
return model.isArmed();
}
Returns whether the menu item is "armed". |
void largeIconChanged(Action a) {
}
|
public void menuSelectionChanged(boolean isIncluded) {
setArmed(isIncluded);
}
Called by the MenuSelectionManager when the
MenuElement is selected or unselected. |
protected String paramString() {
return super.paramString();
}
Returns a string representation of this JMenuItem.
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. |
public void processKeyEvent(KeyEvent e,
MenuElement[] path,
MenuSelectionManager manager) {
if (DEBUG) {
System.out.println("in JMenuItem.processKeyEvent/3 for " + getText() +
" " + KeyStroke.getKeyStrokeForEvent(e));
}
MenuKeyEvent mke = new MenuKeyEvent(e.getComponent(), e.getID(),
e.getWhen(), e.getModifiers(),
e.getKeyCode(), e.getKeyChar(),
path, manager);
processMenuKeyEvent(mke);
if (mke.isConsumed()) {
e.consume();
}
}
Processes a key event forwarded from the
MenuSelectionManager and changes the menu selection,
if necessary, by using MenuSelectionManager's API.
Note: you do not have to forward the event to sub-components.
This is done automatically by the MenuSelectionManager. |
public void processMenuDragMouseEvent(MenuDragMouseEvent e) {
switch (e.getID()) {
case MouseEvent.MOUSE_ENTERED:
isMouseDragged = false; fireMenuDragMouseEntered(e); break;
case MouseEvent.MOUSE_EXITED:
isMouseDragged = false; fireMenuDragMouseExited(e); break;
case MouseEvent.MOUSE_DRAGGED:
isMouseDragged = true; fireMenuDragMouseDragged(e); break;
case MouseEvent.MOUSE_RELEASED:
if(isMouseDragged) fireMenuDragMouseReleased(e); break;
default:
break;
}
}
Handles mouse drag in a menu. |
public void processMenuKeyEvent(MenuKeyEvent e) {
if (DEBUG) {
System.out.println("in JMenuItem.processMenuKeyEvent for " + getText()+
" " + KeyStroke.getKeyStrokeForEvent(e));
}
switch (e.getID()) {
case KeyEvent.KEY_PRESSED:
fireMenuKeyPressed(e); break;
case KeyEvent.KEY_RELEASED:
fireMenuKeyReleased(e); break;
case KeyEvent.KEY_TYPED:
fireMenuKeyTyped(e); break;
default:
break;
}
}
Handles a keystroke in a menu. |
public void processMouseEvent(MouseEvent e,
MenuElement[] path,
MenuSelectionManager manager) {
processMenuDragMouseEvent(
new MenuDragMouseEvent(e.getComponent(), e.getID(),
e.getWhen(),
e.getModifiers(), e.getX(), e.getY(),
e.getXOnScreen(), e.getYOnScreen(),
e.getClickCount(), e.isPopupTrigger(),
path, manager));
}
Processes a mouse event forwarded from the
MenuSelectionManager and changes the menu
selection, if necessary, by using the
MenuSelectionManager's API.
Note: you do not have to forward the event to sub-components.
This is done automatically by the MenuSelectionManager. |
public void removeMenuDragMouseListener(MenuDragMouseListener l) {
listenerList.remove(MenuDragMouseListener.class, l);
}
Removes a MenuDragMouseListener from the menu item. |
public void removeMenuKeyListener(MenuKeyListener l) {
listenerList.remove(MenuKeyListener.class, l);
}
Removes a MenuKeyListener from the menu item. |
public void setAccelerator(KeyStroke keyStroke) {
KeyStroke oldAccelerator = accelerator;
this.accelerator = keyStroke;
repaint();
revalidate();
firePropertyChange("accelerator", oldAccelerator, accelerator);
}
Sets the key combination which invokes the menu item's
action listeners without navigating the menu hierarchy. It is the
UI's responsibility to install the correct action. Note that
when the keyboard accelerator is typed, it will work whether or
not the menu is currently displayed. |
public void setArmed(boolean b) {
ButtonModel model = (ButtonModel) getModel();
boolean oldValue = model.isArmed();
if(model.isArmed() != b) {
model.setArmed(b);
}
}
Identifies the menu item as "armed". If the mouse button is
released while it is over this item, the menu's action event
will fire. If the mouse button is released elsewhere, the
event will not fire and the menu item will be disarmed. |
public void setEnabled(boolean b) {
// Make sure we aren't armed!
if (!b && !UIManager.getBoolean("MenuItem.disabledAreNavigable")) {
setArmed(false);
}
super.setEnabled(b);
}
Enables or disables the menu item. |
void setIconFromAction(Action a) {
Icon icon = null;
if (a != null) {
icon = (Icon)a.getValue(Action.SMALL_ICON);
}
setIcon(icon);
}
|
public void setModel(ButtonModel newModel) {
super.setModel(newModel);
if(newModel instanceof DefaultButtonModel) {
((DefaultButtonModel)newModel).setMenuItem(true);
}
}
|
public void setUI(MenuItemUI ui) {
super.setUI(ui);
}
Sets the look and feel object that renders this component. |
void smallIconChanged(Action a) {
setIconFromAction(a);
}
|
public void updateUI() {
setUI((MenuItemUI)UIManager.getUI(this));
}
Resets the UI property with a value from the current look and feel. |