An implementation of a menu bar. You add
objects to the
menu bar to construct a menu. When the user selects a
on it.
| Methods from java.awt.Component: |
|---|
|
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, dispatchEventImpl, doLayout, enable, enable, enableEvents, enableInputMethods, eventTypeEnabled, findNextFocusComponent, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, generateName, getAccessibleContext, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getGraphicsConfigurationImpl, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFocusable, isFontSet, isForegroundSet, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFocusable, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setPeer, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, translateEvent, update, validate |
| Method from javax.swing.JMenuBar Detail: |
public JMenu add(JMenu c) {
super.add(c);
return c;
}
Appends the specified menu to the end of the menu bar. |
public void addNotify() {
super.addNotify();
KeyboardManager.getCurrentManager().registerMenuBar(this);
}
Overrides JComponent.addNotify to register this
menu bar with the current keyboard manager. |
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleJMenuBar();
}
return accessibleContext;
}
Gets the AccessibleContext associated with this JMenuBar.
For JMenuBars, the AccessibleContext takes the form of an
AccessibleJMenuBar.
A new AccessibleJMenuBar instance is created if necessary. |
public Component getComponent() {
return this;
}
Implemented to be a MenuElement. Returns this object. |
public Component getComponentAtIndex(int i) {
if(i < 0 || i >= getComponentCount()) {
return null;
}
return getComponent(i);
} Deprecated! replaced - by getComponent(int i)
Returns the component at the specified index. |
public int getComponentIndex(Component c) {
int ncomponents = this.getComponentCount();
Component[] component = this.getComponents();
for (int i = 0 ; i < ncomponents ; i++) {
Component comp = component[i];
if (comp == c)
return i;
}
return -1;
}
Returns the index of the specified component. |
public JMenu getHelpMenu() {
throw new Error("getHelpMenu() not yet implemented.");
}
Gets the help menu for the menu bar. This method is not yet
implemented and will throw an exception. |
public Insets getMargin() {
if(margin == null) {
return new Insets(0,0,0,0);
} else {
return margin;
}
}
Returns the margin between the menubar's border and
its menus. If there is no previous margin, it will create
a default margin with zero size. |
public JMenu getMenu(int index) {
Component c = getComponentAtIndex(index);
if (c instanceof JMenu)
return (JMenu) c;
return null;
}
Returns the menu at the specified position in the menu bar. |
public int getMenuCount() {
return getComponentCount();
}
Returns the number of items in the menu bar. |
public SingleSelectionModel getSelectionModel() {
return selectionModel;
}
Returns the model object that handles single selections. |
public MenuElement[] getSubElements() {
MenuElement result[];
Vector tmp = new Vector();
int c = getComponentCount();
int i;
Component m;
for(i=0 ; i < c ; i++) {
m = getComponent(i);
if(m instanceof MenuElement)
tmp.addElement(m);
}
result = new MenuElement[tmp.size()];
for(i=0,c=tmp.size() ; i < c ; i++)
result[i] = (MenuElement) tmp.elementAt(i);
return result;
}
Implemented to be a MenuElement -- returns the
menus in this menu bar.
This is the reason for implementing the MenuElement
interface -- so that the menu bar can be treated the same as
other menu elements. |
public MenuBarUI getUI() {
return (MenuBarUI)ui;
}
Returns the menubar's current UI. |
public String getUIClassID() {
return uiClassID;
}
Returns the name of the L&F class that renders this component. |
public boolean isBorderPainted() {
return paintBorder;
}
Returns true if the menu bars border should be painted. |
public boolean isSelected() {
return selectionModel.isSelected();
}
Returns true if the menu bar currently has a component selected. |
public void menuSelectionChanged(boolean isIncluded) {
}
Implemented to be a MenuElement -- does nothing. |
protected void paintBorder(Graphics g) {
if (isBorderPainted()) {
super.paintBorder(g);
}
}
Paints the menubar's border if BorderPainted
property is true. |
protected String paramString() {
String paintBorderString = (paintBorder ?
"true" : "false");
String marginString = (margin != null ?
margin.toString() : "");
return super.paramString() +
",margin=" + marginString +
",paintBorder=" + paintBorderString;
}
Returns a string representation of this JMenuBar.
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. |
static boolean processBindingForKeyStrokeRecursive(MenuElement elem,
KeyStroke ks,
KeyEvent e,
int condition,
boolean pressed) {
if (elem == null) {
return false;
}
Component c = elem.getComponent();
if ( !(c.isVisible() || (c instanceof JPopupMenu)) || !c.isEnabled() ) {
return false;
}
if (c != null && c instanceof JComponent &&
((JComponent)c).processKeyBinding(ks, e, condition, pressed)) {
return true;
}
MenuElement[] subElements = elem.getSubElements();
for(int i=0; i< subElements.length; i++) {
if (processBindingForKeyStrokeRecursive(subElements[i], ks, e,
condition, pressed)) {
return true;
// We don't, pass along to children JMenu's
}
}
return false;
}
|
protected boolean processKeyBinding(KeyStroke ks,
KeyEvent e,
int condition,
boolean pressed) {
// See if we have a local binding.
boolean retValue = super.processKeyBinding(ks, e, condition, pressed);
if (!retValue) {
MenuElement[] subElements = getSubElements();
for (int i=0; i< subElements.length; i++) {
if (processBindingForKeyStrokeRecursive(
subElements[i], ks, e, condition, pressed)) {
return true;
}
}
}
return retValue;
}
Subclassed to check all the child menus. |
public void processKeyEvent(KeyEvent e,
MenuElement[] path,
MenuSelectionManager manager) {
}
Implemented to be a MenuElement -- does nothing. |
public void processMouseEvent(MouseEvent event,
MenuElement[] path,
MenuSelectionManager manager) {
}
Implemented to be a MenuElement -- does nothing. |
public void removeNotify() {
super.removeNotify();
KeyboardManager.getCurrentManager().unregisterMenuBar(this);
}
Overrides JComponent.removeNotify to unregister this
menu bar with the current keyboard manager. |
public void setBorderPainted(boolean b) {
boolean oldValue = paintBorder;
paintBorder = b;
firePropertyChange("borderPainted", oldValue, paintBorder);
if (b != oldValue) {
revalidate();
repaint();
}
}
Sets whether the border should be painted. |
public void setHelpMenu(JMenu menu) {
throw new Error("setHelpMenu() not yet implemented.");
}
Sets the help menu that appears when the user selects the
"help" option in the menu bar. This method is not yet implemented
and will throw an exception. |
public void setMargin(Insets m) {
Insets old = margin;
this.margin = m;
firePropertyChange("margin", old, m);
if (old == null || !old.equals(m)) {
revalidate();
repaint();
}
}
Sets the margin between the menubar's border and
its menus. Setting to null will cause the menubar to
use the default margins. |
public void setSelected(Component sel) {
SingleSelectionModel model = getSelectionModel();
int index = getComponentIndex(sel);
model.setSelectedIndex(index);
}
Sets the currently selected component, producing a
a change to the selection model. |
public void setSelectionModel(SingleSelectionModel model) {
SingleSelectionModel oldValue = selectionModel;
this.selectionModel = model;
firePropertyChange("selectionModel", oldValue, selectionModel);
}
Sets the model object to handle single selections. |
public void setUI(MenuBarUI ui) {
super.setUI(ui);
}
Sets the L&F object that renders this component. |
public void updateUI() {
setUI((MenuBarUI)UIManager.getUI(this));
}
Resets the UI property with a value from the current look and feel. |