- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- javax.swing.JComponent
-
- javax.swing.JMenuBar
-
- All Implemented Interfaces:
- ImageObserver, MenuContainer, Serializable, Accessible, MenuElement
- Direct Known Subclasses:
- BasicInternalFrameTitlePane.SystemMenuBar
public class JMenuBar extends JComponent implements Accessible, MenuElement
An implementation of a menu bar. You addJMenu
objects to the menu bar to construct a menu. When the user selects aJMenu
object, its associatedJPopupMenu
is displayed, allowing the user to select one of theJMenuItems
on it.For information and examples of using menu bars see How to Use Menus, a section in The Java Tutorial.
Warning: Swing is not thread safe. For more information see Swing's Threading Policy.
Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the
java.beans
package. Please seeXMLEncoder
.- See Also:
JMenu
,JPopupMenu
,JMenuItem
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description protected class
JMenuBar.AccessibleJMenuBar
This class implements accessibility support for theJMenuBar
class.-
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
-
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
-
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
-
Field Summary
-
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
-
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
-
-
Constructor Summary
Constructors Constructor and Description JMenuBar()
Creates a new menu bar.
-
Method Summary
Methods Modifier and Type Method and Description JMenu
add(JMenu c)
Appends the specified menu to the end of the menu bar.void
addNotify()
OverridesJComponent.addNotify
to register this menu bar with the current keyboard manager.AccessibleContext
getAccessibleContext()
Gets the AccessibleContext associated with this JMenuBar.Component
getComponent()
Implemented to be aMenuElement
.Component
getComponentAtIndex(int i)
Deprecated.replaced bygetComponent(int i)
int
getComponentIndex(Component c)
Returns the index of the specified component.JMenu
getHelpMenu()
Gets the help menu for the menu bar.Insets
getMargin()
Returns the margin between the menubar's border and its menus.JMenu
getMenu(int index)
Returns the menu at the specified position in the menu bar.int
getMenuCount()
Returns the number of items in the menu bar.SingleSelectionModel
getSelectionModel()
Returns the model object that handles single selections.MenuElement[]
getSubElements()
Implemented to be aMenuElement
-- returns the menus in this menu bar.MenuBarUI
getUI()
Returns the menubar's current UI.String
getUIClassID()
Returns the name of the L&F class that renders this component.boolean
isBorderPainted()
Returns true if the menu bars border should be painted.boolean
isSelected()
Returns true if the menu bar currently has a component selected.void
menuSelectionChanged(boolean isIncluded)
Implemented to be aMenuElement
-- does nothing.protected void
paintBorder(Graphics g)
Paints the menubar's border ifBorderPainted
property is true.protected String
paramString()
Returns a string representation of thisJMenuBar
.protected boolean
processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
Subclassed to check all the child menus.void
processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)
Implemented to be aMenuElement
-- does nothing.void
processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)
Implemented to be aMenuElement
-- does nothing.void
removeNotify()
OverridesJComponent.removeNotify
to unregister this menu bar with the current keyboard manager.void
setBorderPainted(boolean b)
Sets whether the border should be painted.void
setHelpMenu(JMenu menu)
Sets the help menu that appears when the user selects the "help" option in the menu bar.void
setMargin(Insets m)
Sets the margin between the menubar's border and its menus.void
setSelected(Component sel)
Sets the currently selected component, producing a a change to the selection model.void
setSelectionModel(SingleSelectionModel model)
Sets the model object to handle single selections.void
setUI(MenuBarUI ui)
Sets the L&F object that renders this component.void
updateUI()
Resets the UI property with a value from the current look and feel.-
Methods inherited from class javax.swing.JComponent
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
-
Methods inherited from class java.awt.Container
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
-
Methods inherited from class java.awt.Component
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
-
-
-
-
Method Detail
-
getUI
public MenuBarUI getUI()
Returns the menubar's current UI.- See Also:
setUI(javax.swing.plaf.MenuBarUI)
-
setUI
public void setUI(MenuBarUI ui)
Sets the L&F object that renders this component.- Parameters:
ui
- the new MenuBarUI L&F object- See Also:
UIDefaults.getUI(javax.swing.JComponent)
-
updateUI
public void updateUI()
Resets the UI property with a value from the current look and feel.- Overrides:
updateUI
in classJComponent
- See Also:
JComponent.updateUI()
-
getUIClassID
public String getUIClassID()
Returns the name of the L&F class that renders this component.- Overrides:
getUIClassID
in classJComponent
- Returns:
- the string "MenuBarUI"
- See Also:
JComponent.getUIClassID()
,UIDefaults.getUI(javax.swing.JComponent)
-
getSelectionModel
public SingleSelectionModel getSelectionModel()
Returns the model object that handles single selections.- Returns:
- the
SingleSelectionModel
property - See Also:
SingleSelectionModel
-
setSelectionModel
public void setSelectionModel(SingleSelectionModel model)
Sets the model object to handle single selections.- Parameters:
model
- theSingleSelectionModel
to use- See Also:
SingleSelectionModel
-
add
public JMenu add(JMenu c)
Appends the specified menu to the end of the menu bar.- Parameters:
c
- theJMenu
component to add- Returns:
- the menu component
-
getMenu
public JMenu getMenu(int index)
Returns the menu at the specified position in the menu bar.- Parameters:
index
- an integer giving the position in the menu bar, where 0 is the first position- Returns:
- the
JMenu
at that position, ornull
if if there is noJMenu
at that position (ie. if it is aJMenuItem
)
-
getMenuCount
public int getMenuCount()
Returns the number of items in the menu bar.- Returns:
- the number of items in the menu bar
-
setHelpMenu
public void setHelpMenu(JMenu menu)
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.- Parameters:
menu
- the JMenu that delivers help to the user
-
getHelpMenu
public JMenu getHelpMenu()
Gets the help menu for the menu bar. This method is not yet implemented and will throw an exception.- Returns:
- the
JMenu
that delivers help to the user
-
getComponentAtIndex
@Deprecated public Component getComponentAtIndex(int i)
Deprecated. replaced bygetComponent(int i)
Returns the component at the specified index.- Parameters:
i
- an integer specifying the position, where 0 is first- Returns:
- the
Component
at the position, ornull
for an invalid index
-
getComponentIndex
public int getComponentIndex(Component c)
Returns the index of the specified component.- Parameters:
c
- theComponent
to find- Returns:
- an integer giving the component's position, where 0 is first; or -1 if it can't be found
-
setSelected
public void setSelected(Component sel)
Sets the currently selected component, producing a a change to the selection model.- Parameters:
sel
- theComponent
to select
-
isSelected
public boolean isSelected()
Returns true if the menu bar currently has a component selected.- Returns:
- true if a selection has been made, else false
-
isBorderPainted
public boolean isBorderPainted()
Returns true if the menu bars border should be painted.- Returns:
- true if the border should be painted, else false
-
setBorderPainted
public void setBorderPainted(boolean b)
Sets whether the border should be painted.- Parameters:
b
- if true and border property is notnull
, the border is painted.- See Also:
isBorderPainted()
-
paintBorder
protected void paintBorder(Graphics g)
Paints the menubar's border ifBorderPainted
property is true.- Overrides:
paintBorder
in classJComponent
- Parameters:
g
- theGraphics
context to use for painting- See Also:
JComponent.paint(java.awt.Graphics)
,JComponent.setBorder(javax.swing.border.Border)
-
setMargin
public void setMargin(Insets m)
Sets the margin between the menubar's border and its menus. Setting tonull
will cause the menubar to use the default margins.- Parameters:
m
- an Insets object containing the margin values- See Also:
Insets
-
getMargin
public Insets getMargin()
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.- Returns:
- an
Insets
object containing the margin values - See Also:
Insets
-
processMouseEvent
public void processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)
Implemented to be aMenuElement
-- does nothing.- Specified by:
processMouseEvent
in interfaceMenuElement
- See Also:
getSubElements()
-
processKeyEvent
public void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)
Implemented to be aMenuElement
-- does nothing.- Specified by:
processKeyEvent
in interfaceMenuElement
- See Also:
getSubElements()
-
menuSelectionChanged
public void menuSelectionChanged(boolean isIncluded)
Implemented to be aMenuElement
-- does nothing.- Specified by:
menuSelectionChanged
in interfaceMenuElement
- See Also:
getSubElements()
-
getSubElements
public MenuElement[] getSubElements()
Implemented to be aMenuElement
-- returns the menus in this menu bar. This is the reason for implementing theMenuElement
interface -- so that the menu bar can be treated the same as other menu elements.- Specified by:
getSubElements
in interfaceMenuElement
- Returns:
- an array of menu items in the menu bar.
-
getComponent
public Component getComponent()
Implemented to be aMenuElement
. Returns this object.- Specified by:
getComponent
in interfaceMenuElement
- Returns:
- the current
Component
(this) - See Also:
getSubElements()
-
paramString
protected String paramString()
Returns a string representation of thisJMenuBar
. 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 benull
.- Overrides:
paramString
in classJComponent
- Returns:
- a string representation of this
JMenuBar
-
getAccessibleContext
public AccessibleContext getAccessibleContext()
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.- Specified by:
getAccessibleContext
in interfaceAccessible
- Overrides:
getAccessibleContext
in classJComponent
- Returns:
- an AccessibleJMenuBar that serves as the AccessibleContext of this JMenuBar
-
processKeyBinding
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
Subclassed to check all the child menus.- Overrides:
processKeyBinding
in classJComponent
- Parameters:
ks
- theKeyStroke
queriede
- theKeyEvent
condition
- one of the following values:- JComponent.WHEN_FOCUSED
- JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
- JComponent.WHEN_IN_FOCUSED_WINDOW
pressed
- true if the key is pressed- Returns:
- true if there was a binding to an action, and the action was enabled
- Since:
- 1.3
-
addNotify
public void addNotify()
OverridesJComponent.addNotify
to register this menu bar with the current keyboard manager.
-
removeNotify
public void removeNotify()
OverridesJComponent.removeNotify
to unregister this menu bar with the current keyboard manager.
-
-
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 11/06/2005 gemaakt, de laatste keer de 04/03/2020 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/java-api-rf-javax/swing/JMenuBar.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.