SWING - Menu Classes-JMenuBar Class - Java Swing

What is SWING JMenuBar Class?

Introduction

The JMenuBar class provides an implementation of a menu bar.

Class Declaration

Following is the declaration for javax.swing.JMenuBar class −

Class Constructors

Sr.No. Constructor & Description
1
JMenuBar()
Creates a new menu bar.

Class Methods

Here is the list of methods in Swing JMenuBar Control class.

Sr.No. Method & Description
1
JMenu add(JMenu c)
Appends the specified menu to the end of the menu bar.
2
void addNotify()
Overrides JComponent.addNotify to register this menu bar with the current keyboard manager.
3
AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JMenuBar.
4
Component getComponent()
Implemented to be a MenuElement.
5
Component getComponentAtIndex(int i)
Deprecated. Replaced by getComponent(int i)
6
int getComponentIndex(Component c)
Returns the index of the specified component.
7
JMenu getHelpMenu()
Gets the help menu for the menu bar.
8
Insets getMargin()
Returns the margin between the menubar's border and its menus.
9
JMenu getMenu(int index)
Returns the menu at the specified position in the menu bar.
10
int getMenuCount()
Returns the number of items in the menu bar.
11
SingleSelectionModel getSelectionModel()
Returns the model object that handles single selections.
12
MenuElement[] getSubElements()
Implemented to be a MenuElement. Returns the menus in this menu bar.
13
MenuBarUI getUI()
Returns the menubar's current UI.
14
String getUIClassID()
Returns the name of the L&F class that renders this component.
15
boolean isBorderPainted()
Returns true if the menu bars border should be painted.
16
boolean isSelected()
Returns true if the menu bar currently has a component selected.
17
void menuSelectionChanged(boolean isIncluded)
Implemented to be a MenuElement, does nothing.
18
protected void paintBorder(Graphics g)
Paints the menubar's border, if the BorderPainted property is true.
19
protected String paramString()
Returns a string representation of this JMenuBar.
20
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
Subclassed to check all the child menus.
21
void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)
Implemented to be a MenuElement, does nothing.
22
void processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)
Implemented to be a MenuElement, does nothing.
23
void removeNotify()
Overrides JComponent.removeNotify to unregister this menu bar with the current keyboard manager.
24
void setBorderPainted(boolean b)
Sets whether the border should be painted.
25
void setHelpMenu(JMenu menu)
Sets the help menu that appears when the user selects the "help" option in the menu bar.
26
void setMargin(Insets m)
Sets the margin between the menubar's border and its menus.
27
void setSelected(Component sel)
Sets the currently selected component, producing a change to the selection model.
28
void setSelectionModel(SingleSelectionModel model)
Sets the model object to handle single selections.
29
void setUI(MenuBarUI ui)
Sets the L&F object that renders this component.
30
void updateUI()
Resets the UI property with a value from the current look and feel.

Methods Inherited

This class inherits methods from the following classes −

  • javax.swing.JComponent
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

JMenuBar Example

Create the following Java program using any editor of your choice in say D:/ > SWING > com > wisdomjobs> gui >

SwingMenuDemo.java

Compile the program using the command prompt. Go to D:/ > SWING and type the following command.

If no error occurs, it means the compilation is successful. Run the program using the following command.

Verify the following output.

swing_jmenubar

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Swing Topics