public class ProgramMenuBar
extends javax.swing.JMenuBar
implements java.lang.Iterable<javax.swing.JMenuItem>
These goals turn out to be difficult to achieve simultaneously. In
particular, supporting both Macintosh-style and Windows-style menu bars
requires creating a parallel MenuBar
structure behind the
underlying JMenuBar
, which accounts for much of the complexity
in this implementation.
Using the ProgramMenuBar
class
The ProgramMenuBar
class supports two distinct disciplines for
listening for menu actions:
createFocusedItem
.
Activating a focused item passes an action event to the listener set by
calling setFocusedListener
,
which should be called whenever a component interested in responding to menu
actions gains the keyboard focus.
createProgramItem
.
Activating a program item passes an action event to the
menuAction
method in the Program
object that
created the menu bar.
javax.swing.JMenuBar.AccessibleJMenuBar
javax.swing.JComponent.AccessibleJComponent
Modifier and Type | Field and Description |
---|---|
static int |
SHIFT
Constant indicating that an accelerator key requires the SHIFT modifier.
|
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Constructor and Description |
---|
ProgramMenuBar(Program owner)
Creates an empty
ProgramMenuBar . |
Modifier and Type | Method and Description |
---|---|
protected void |
addEditMenu()
Installs the
Edit menu. |
protected void |
addEditMenuItems(javax.swing.JMenu menu)
Adds the standard
Edit items to the specified menu. |
protected void |
addFileMenu()
Installs the
File menu. |
protected void |
addFileMenuItems(javax.swing.JMenu menu)
Adds the standard
File items to the specified menu. |
protected void |
addMenus()
Adds menus to the menu bar.
|
javax.swing.JMenuItem |
createFocusedItem(java.lang.String action)
Creates a focused menu item with the specified action command.
|
javax.swing.JMenuItem |
createFocusedItem(java.lang.String action,
int key)
Creates a focused menu item with the specified action command and accelerator
key.
|
javax.swing.JMenuItem |
createProgramItem(java.lang.String action)
Creates a program menu item with the specified action command.
|
javax.swing.JMenuItem |
createProgramItem(java.lang.String action,
int key)
Creates a program menu item with the specified action command and accelerator
key.
|
javax.swing.JMenuItem |
createStandardItem(java.lang.String action)
Creates one of the standard menu items implemented by the
ProgramMenuBar class. |
boolean |
fireAccelerator(java.awt.event.KeyEvent e)
Triggers the accelerator associated with the keystroke implied by the key
event.
|
void |
fireActionListeners(java.awt.event.ActionEvent e)
Fires the action listeners responsible for handling the specified event.
|
boolean |
getMacMenuBarFlag()
Retrieves the setting of the Mac menu bar flag.
|
Program |
getProgram()
Returns the
Program object associated with this menu. |
void |
install(javax.swing.JFrame frame)
Installs the menu bar in the
JFrame or Program
object enclosing the component comp . |
boolean |
isFocusedItem(javax.swing.JMenuItem item)
Returns
true if the item is a focused item. |
java.util.Iterator<javax.swing.JMenuItem> |
iterator()
Returns an iterator that enumerates the individual menu items under the
control of the menu bar.
|
void |
setAccelerator(javax.swing.JMenuItem item,
int key)
Sets the accelerator for the item as appropriate to the operating system
conventions.
|
void |
setEnabled(java.lang.String action,
boolean flag)
Enables or disables any menu items that generate the specified action
command.
|
void |
setFocusedListener(java.awt.event.ActionListener listener)
Registers a listener that responds while the caller holds the keyboard focus.
|
void |
setMacMenuBarFlag(boolean flag)
Sets a flag indicating whether applications running on the Macintosh should
use standard Mac menus.
|
add, addNotify, getAccessibleContext, getComponent, getComponentAtIndex, getComponentIndex, getHelpMenu, getMargin, getMenu, getMenuCount, getSelectionModel, getSubElements, getUI, getUIClassID, isBorderPainted, isSelected, menuSelectionChanged, paintBorder, paramString, processKeyBinding, processKeyEvent, processMouseEvent, removeNotify, setBorderPainted, setHelpMenu, setMargin, setSelected, setSelectionModel, setUI, updateUI
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
public static final int SHIFT
public ProgramMenuBar(Program owner)
ProgramMenuBar
.owner
- The Program
that owns this menu bar.public Program getProgram()
Program
object associated with this menu.public javax.swing.JMenuItem createStandardItem(java.lang.String action)
ProgramMenuBar
class. The menu item is identified by its action
command.action
- The action command identifying the menu item to be createdpublic javax.swing.JMenuItem createProgramItem(java.lang.String action)
setName
on the item.action
- The action command generated by this menu itempublic javax.swing.JMenuItem createProgramItem(java.lang.String action, int key)
action
- The action command generated by this menu itemkey
- The integer value of the keystroke acceleratorpublic javax.swing.JMenuItem createFocusedItem(java.lang.String action)
action
- The action command generated by this menu itempublic javax.swing.JMenuItem createFocusedItem(java.lang.String action, int key)
action
- The action command generated by this menu itemkey
- The integer value of the keystroke acceleratorpublic boolean isFocusedItem(javax.swing.JMenuItem item)
true
if the item is a focused item.item
- A menu item installed in the menu bartrue
if the item is a program itempublic void setAccelerator(javax.swing.JMenuItem item, int key)
item
- The menu item triggered by this acceleratorkey
- The integer value of the keystroke acceleratorpublic void setEnabled(java.lang.String action, boolean flag)
action
- The action command triggered by the menu itemflag
- true
to enable the item, false
to
disable itpublic void install(javax.swing.JFrame frame)
JFrame
or Program
object enclosing the component comp
.comp
- A descendant of the frame in which the menu is to be installedpublic void setMacMenuBarFlag(boolean flag)
true
. Setting this value
to false
means that Mac programs use the same in-window
JMenuBar
approach used on other platforms.flag
- true
to use Mac menu style; false
otherwisepublic boolean getMacMenuBarFlag()
true
if Mac menu style is supported; false
otherwisepublic void fireActionListeners(java.awt.event.ActionEvent e)
public boolean fireAccelerator(java.awt.event.KeyEvent e)
true
if such an accelerator exists.public void setFocusedListener(java.awt.event.ActionListener listener)
null
when it loses it.listener
- An ActionListener
that responds to focused itemspublic java.util.Iterator<javax.swing.JMenuItem> iterator()
iterator
in interface java.lang.Iterable<javax.swing.JMenuItem>
protected void addMenus()
File
and Edit
menus
should override this method with one that adds the desired menus.protected void addFileMenu()
File
menu.protected void addEditMenu()
Edit
menu.protected void addFileMenuItems(javax.swing.JMenu menu)
File
items to the specified menu. Subclasses
can override this method to change the list of items.menu
- The menu to which the File
items are addedprotected void addEditMenuItems(javax.swing.JMenu menu)
Edit
items to the specified menu. Subclasses
can override this method to change the list of items.menu
- The menu to which the Edit
items are added