Introduction

The MenuBar class provides menu bar bound to a frame and is platform specific.

Class declaration

Following is the declaration for java.awt.MenuBar class:

  1. public class MenuBar
  2. extends MenuComponent
  3. implements MenuContainer, Accessible

Class constructors

S.N.Constructor & Description
1 MenuBar() Creates a new menu bar.

Class methods

S.N.Method & Description
1 void dispatchEvent(AWTEvent e)
2 Menu add(Menu m) Adds the specified menu to the menu bar.
3 void addNotify() Creates the menu bar's peer.
4 int countMenus() Deprecated. As of JDK version 1.1, replaced by getMenuCount().
5 void deleteShortcut(MenuShortcut s) Deletes the specified menu shortcut.
6 AccessibleContext getAccessibleContext() Gets the AccessibleContext associated with this MenuBar.
7 Menu getHelpMenu() Gets the help menu on the menu bar.
8 Menu getMenu(int i) Gets the specified menu.
9 int getMenuCount() Gets the number of menus on the menu bar.
10 MenuItem getShortcutMenuItem(MenuShortcut s) Gets the instance of MenuItem associated with the specified MenuShortcut object, or null if none of the menu items being managed by this menu bar is associated with the specified menu shortcut.
11 void remove(int index) Removes the menu located at the specified index from this menu bar.
12 void remove(MenuComponent m) Removes the specified menu component from this menu bar.
13 void removeNotify() Removes the menu bar's peer.
14 void setHelpMenu(Menu m) Sets the specified menu to be this menu bar's help menu.
15 Enumeration shortcuts() Gets an enumeration of all menu shortcuts this menu bar is managing.

Methods inherited

This class inherits methods from the following classes:

  • java.awt.MenuComponent

  • java.lang.Object

MenuBar Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui > AWTMenuDemo.java

  1. package com.tutorialspoint.gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class AWTMenuDemo {
  7. private Frame mainFrame;
  8. private Label headerLabel;
  9. private Label statusLabel;
  10. private Panel controlPanel;
  11.  
  12. public AWTMenuDemo(){
  13. prepareGUI();
  14. }
  15.  
  16. public static void main(String[] args){
  17. AWTMenuDemo awtMenuDemo = new AWTMenuDemo();
  18. awtMenuDemo.showMenuDemo();
  19. }
  20.  
  21. private void prepareGUI(){
  22. mainFrame = new Frame("Java AWT Examples");
  23. mainFrame.setSize(400,400);
  24. mainFrame.setLayout(new GridLayout(3, 1));
  25. mainFrame.addWindowListener(new WindowAdapter() {
  26. public void windowClosing(WindowEvent windowEvent){
  27. System.exit(0);
  28. }
  29. });
  30. headerLabel = new Label();
  31. headerLabel.setAlignment(Label.CENTER);
  32. statusLabel = new Label();
  33. statusLabel.setAlignment(Label.CENTER);
  34. statusLabel.setSize(350,100);
  35.  
  36. controlPanel = new Panel();
  37. controlPanel.setLayout(new FlowLayout());
  38.  
  39. mainFrame.add(headerLabel);
  40. mainFrame.add(controlPanel);
  41. mainFrame.add(statusLabel);
  42. mainFrame.setVisible(true);
  43. }
  44.  
  45. private void showMenuDemo(){
  46. //create a menu bar
  47. final MenuBar menuBar = new MenuBar();
  48.  
  49. //create menus
  50. Menu fileMenu = new Menu("File");
  51. Menu editMenu = new Menu("Edit");
  52. final Menu aboutMenu = new Menu("About");
  53.  
  54. //create menu items
  55. MenuItem newMenuItem =
  56. new MenuItem("New",new MenuShortcut(KeyEvent.VK_N));
  57. newMenuItem.setActionCommand("New");
  58.  
  59. MenuItem openMenuItem = new MenuItem("Open");
  60. openMenuItem.setActionCommand("Open");
  61.  
  62. MenuItem saveMenuItem = new MenuItem("Save");
  63. saveMenuItem.setActionCommand("Save");
  64.  
  65. MenuItem exitMenuItem = new MenuItem("Exit");
  66. exitMenuItem.setActionCommand("Exit");
  67.  
  68. MenuItem cutMenuItem = new MenuItem("Cut");
  69. cutMenuItem.setActionCommand("Cut");
  70.  
  71. MenuItem copyMenuItem = new MenuItem("Copy");
  72. copyMenuItem.setActionCommand("Copy");
  73.  
  74. MenuItem pasteMenuItem = new MenuItem("Paste");
  75. pasteMenuItem.setActionCommand("Paste");
  76.  
  77. MenuItemListener menuItemListener = new MenuItemListener();
  78.  
  79. newMenuItem.addActionListener(menuItemListener);
  80. openMenuItem.addActionListener(menuItemListener);
  81. saveMenuItem.addActionListener(menuItemListener);
  82. exitMenuItem.addActionListener(menuItemListener);
  83. cutMenuItem.addActionListener(menuItemListener);
  84. copyMenuItem.addActionListener(menuItemListener);
  85. pasteMenuItem.addActionListener(menuItemListener);
  86.  
  87. final CheckboxMenuItem showWindowMenu =
  88. new CheckboxMenuItem("Show About", true);
  89. showWindowMenu.addItemListener(new ItemListener() {
  90. public void itemStateChanged(ItemEvent e) {
  91. if(showWindowMenu.getState()){
  92. menuBar.add(aboutMenu);
  93. }else{
  94. menuBar.remove(aboutMenu);
  95. }
  96. }
  97. });
  98.  
  99. //add menu items to menus
  100. fileMenu.add(newMenuItem);
  101. fileMenu.add(openMenuItem);
  102. fileMenu.add(saveMenuItem);
  103. fileMenu.addSeparator();
  104. fileMenu.add(showWindowMenu);
  105. fileMenu.addSeparator();
  106. fileMenu.add(exitMenuItem);
  107.  
  108. editMenu.add(cutMenuItem);
  109. editMenu.add(copyMenuItem);
  110. editMenu.add(pasteMenuItem);
  111.  
  112. //add menu to menubar
  113. menuBar.add(fileMenu);
  114. menuBar.add(editMenu);
  115. menuBar.add(aboutMenu);
  116.  
  117. //add menubar to the frame
  118. mainFrame.setMenuBar(menuBar);
  119. mainFrame.setVisible(true);
  120. }
  121.  
  122. class MenuItemListener implements ActionListener {
  123. public void actionPerformed(ActionEvent e) {
  124. statusLabel.setText(e.getActionCommand()
  125. + " MenuItem clicked.");
  126. }
  127. }
  128. }

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

  1. D:\AWT>javac com\tutorialspoint\gui\AWTMenuDemo.java

If no error comes that means compilation is successful. Run the program using following command.

  1. D:\AWT>java com.tutorialspoint.gui.AWTMenuDemo

Verify the following output

awt_menubar.jpg