public class ProgramMenuBar extends JMenuBar implements Iterable<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.
JMenuBar.AccessibleJMenuBar
JComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor and Description |
---|
ProgramMenuBar(Program owner)
Creates an empty
ProgramMenuBar . |
Modifier and Type | Method and Description |
---|---|
protected void |
addConsoleOptionsMenu()
Installs the
Options menu for console programs. |
protected void |
addEditMenu()
Installs the
Edit menu. |
protected void |
addEditMenuItems(JMenu menu)
Adds the standard
Edit items to the specified menu. |
protected void |
addFileMenu()
Installs the
File menu. |
protected void |
addFileMenuItems(JMenu menu)
Adds the standard
File items to the specified menu. |
protected void |
addGraphicsOptionsMenu()
Installs the
Options menu for graphics programs. |
protected void |
addHelpMenu() |
protected void |
addKarelOptionsMenu()
Installs the
Options menu for Karel programs. |
protected void |
addMenus()
Adds menus to the menu bar.
|
JMenuItem |
createFocusedItem(String action)
Creates a focused menu item with the specified action command.
|
JMenuItem |
createFocusedItem(String action,
int key)
Creates a focused menu item with the specified action command and
accelerator key.
|
JMenuItem |
createFocusedItem(String action,
int key,
KeyStroke keystroke) |
JMenuItem |
createFocusedItem(String action,
int key,
KeyStroke keystroke,
boolean shouldSetAccelerator) |
JMenuItem |
createProgramItem(String action)
Creates a program menu item with the specified action command.
|
JMenuItem |
createProgramItem(String action,
int key) |
JMenuItem |
createProgramItem(String action,
int key,
KeyStroke keystroke)
Creates a program menu item with the specified action command and
accelerator key.
|
JMenuItem |
createStandardItem(String action,
int mnemonic)
Creates one of the standard menu items implemented by the
ProgramMenuBar class. |
JMenuItem |
createStandardItem(String action,
int mnemonic,
KeyStroke keystroke) |
boolean |
fireAccelerator(KeyEvent e)
Triggers the accelerator associated with the keystroke implied by the key
event.
|
void |
fireActionListeners(ActionEvent e)
Fires the action listeners responsible for handling the specified event.
|
<T extends JMenuItem> |
getMenuItem(String text) |
Program |
getProgram()
Returns the
Program object associated with this menu. |
void |
install(Component comp)
Installs the menu bar in the
JFrame or Program
object enclosing the component comp . |
boolean |
isFocusedItem(JMenuItem item)
Returns
true if the item is a focused item. |
Iterator<JMenuItem> |
iterator()
Returns an iterator that enumerates the individual menu items under the
control of the menu bar.
|
void |
setAccelerator(JMenuItem item,
int key)
Sets the accelerator for the item as appropriate to the operating system
conventions.
|
void |
setEnabled(String action,
boolean flag)
Enables or disables any menu items that generate the specified action
command.
|
void |
setFocusedListener(ActionListener listener)
Registers a listener that responds while the caller holds the keyboard
focus.
|
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
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
forEach, spliterator
public static final String MENU_ITEM_TEXT_ABOUT
public static final String MENU_ITEM_TEXT_ANTI_ALIASING
public static final String MENU_ITEM_TEXT_BACKGROUND_COLOR
public static final String MENU_ITEM_TEXT_CHECK_FOR_UPDATES
public static final String MENU_ITEM_TEXT_CLEAR_CONSOLE
public static final String MENU_ITEM_TEXT_COPY
public static final String MENU_ITEM_TEXT_CUT
public static final String MENU_ITEM_TEXT_EXPORT_APPLET
public static final String MENU_ITEM_TEXT_FONT
public static final String MENU_ITEM_TEXT_FOREGROUND_COLOR
public static final String MENU_ITEM_TEXT_PASTE
public static final String MENU_ITEM_TEXT_PRINT
public static final String MENU_ITEM_TEXT_PRINT_CONSOLE
public static final String MENU_ITEM_TEXT_QUIT
public static final String MENU_ITEM_TEXT_SAVE
public static final String MENU_ITEM_TEXT_SAVE_AS
public static final String MENU_ITEM_TEXT_SCRIPT
public static final String MENU_ITEM_TEXT_SELECT_ALL
public static final String MENU_ITEM_TEXT_SHOW_PIXEL_GRID
public static final String MENU_ITEM_TEXT_SHOW_PIXEL_INFO
public static final String MENU_ITEM_TEXT_SUBMIT_PROJECT
public static final String MENU_ITEM_TEXT_INTERACTIVE
public static final String MENU_ITEM_TEXT_MSKAREL
public static final String MENU_ITEM_TEXT_COMPARE_OUTPUT
public static final String MENU_ITEM_TEXT_LOAD_INPUT_SCRIPT
public static final int SHIFT
protected KeyStroke ALT_F4
protected KeyStroke COMMAND_A
protected KeyStroke COMMAND_B
protected KeyStroke COMMAND_C
protected KeyStroke COMMAND_END
protected KeyStroke COMMAND_EQUALS
protected KeyStroke COMMAND_HOME
protected KeyStroke COMMAND_I
protected KeyStroke COMMAND_L
protected KeyStroke COMMAND_MINUS
protected KeyStroke COMMAND_SHIFT_MINUS
protected KeyStroke COMMAND_P
protected KeyStroke COMMAND_PLUS
protected KeyStroke COMMAND_SHIFT_PLUS
protected KeyStroke COMMAND_Q
protected KeyStroke COMMAND_S
protected KeyStroke COMMAND_V
protected KeyStroke COMMAND_W
protected KeyStroke COMMAND_X
protected KeyStroke CTRL_A
protected KeyStroke CTRL_B
protected KeyStroke CTRL_C
protected KeyStroke CTRL_END
protected KeyStroke CTRL_EQUALS
protected KeyStroke CTRL_HOME
protected KeyStroke CTRL_I
protected KeyStroke CTRL_L
protected KeyStroke CTRL_MINUS
protected KeyStroke CTRL_SHIFT_MINUS
protected KeyStroke CTRL_P
protected KeyStroke CTRL_Q
protected KeyStroke CTRL_PLUS
protected KeyStroke CTRL_SHIFT_PLUS
protected KeyStroke CTRL_S
protected KeyStroke CTRL_V
protected KeyStroke CTRL_W
protected KeyStroke CTRL_X
protected KeyStroke DOWN_ARROW
protected KeyStroke F1
protected KeyStroke PGDN
protected KeyStroke PGUP
protected KeyStroke UP_ARROW
public ProgramMenuBar(Program owner)
ProgramMenuBar
.owner
- The Program
that owns this menu bar.public Program getProgram()
Program
object associated with this menu.public JMenuItem createStandardItem(String action, int mnemonic)
ProgramMenuBar
class. The menu item is identified by its
action command.action
- The action command identifying the menu item to be createdpublic JMenuItem createStandardItem(String action, int mnemonic, KeyStroke keystroke)
public JMenuItem createProgramItem(String action)
setName
on the item.action
- The action command generated by this menu itempublic JMenuItem createProgramItem(String action, int key, KeyStroke keystroke)
action
- The action command generated by this menu itemkey
- The integer value of the keystroke acceleratorpublic JMenuItem createFocusedItem(String action)
action
- The action command generated by this menu itempublic JMenuItem createFocusedItem(String action, int key)
action
- The action command generated by this menu itemkey
- The integer value of the keystroke acceleratorpublic JMenuItem createFocusedItem(String action, int key, KeyStroke keystroke, boolean shouldSetAccelerator)
public boolean isFocusedItem(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(JMenuItem item, int key)
item
- The menu item triggered by this acceleratorkey
- The integer value of the keystroke acceleratorpublic void setEnabled(String action, boolean flag)
action
- The action command triggered by the menu itemflag
- true
to enable the item, false
to
disable itpublic void install(Component comp)
JFrame
or Program
object enclosing the component comp
.comp
- A descendant of the frame in which the menu is to be installedpublic void fireActionListeners(ActionEvent e)
public boolean fireAccelerator(KeyEvent e)
true
if such an accelerator
exists.public void setFocusedListener(ActionListener listener)
null
when it loses it.listener
- An ActionListener
that responds to focused itemspublic Iterator<JMenuItem> iterator()
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 addConsoleOptionsMenu()
Options
menu for console programs.protected void addGraphicsOptionsMenu()
Options
menu for graphics programs.protected void addKarelOptionsMenu()
Options
menu for Karel programs.protected void addHelpMenu()
protected void addFileMenuItems(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(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