by Dinesh Thakur Category: AWT and Applets

Creating a menu bar is straight forward. You have to simply create an instance of Java.awt.MenuBar which is a container for menus. It has only one constructor which is the default constructor as follows,

public MenuBar ()

Once you create the menu bar, you can add it to a frame with the setMenuBar () method of Frame class. One should remember that each application has only one menu.

The following statements create a menu bar and add it to the top of the frame.

Menubar menuBar = new MenuBar();


These statements are part of the class that extends Frame.

Initially the menubar is empty and you need to add menus before using it. Each menu is represented by an instance of class Menu. In order to create a Menu instance, you have to use the following constructor.

public Menu(String str)

where str represents the label for the menu. For example: In order to create a File menu, use the following statement

Menu menuFile = new Menu(“File");

Similarly to create Edit and View menu, use the statements

Menu menuEdit = new Menu(“Edit");

Menu menuView = new Menu ("View");

Once the Menu objects are created, we need to add them to the menu bar. For this, you have to use

the add () method of the MenuBar class whose syntax is as follows,

Menu add (Menu menu)

where menu is Menu instance that is added to the menu bar. This method returns a reference to the menu. By default, consecutively added menus are positioned in the menu bar from left to right. This makes the first menu added the leftmost menu and the last menu added the rightmost menu. If you want to add a menu at a specific location, then use the following version of add ()method inherited from the Container class.

Component add (Component menu, int index)

where menu is added to the menu bar at the specified index. Indexing begins at 0, with 0 being the leftmost menu. For example: In order to add menu instance menuFile to the menuBar use the following statement,


Similarly, add other Menu instances menuEdit,menuView using the following statements,



import java.awt.*;

class MenuExample extends Frame
           MenuBar menuBar = new MenuBar();
           Menu menuFile = new Menu("File");
           Menu menuEdit = new Menu("Edit");
           Menu menuView = new Menu("View");
           MenuItem itemOpen = new MenuItem("Open");
           MenuItem itemSave = new MenuItem("Save");
           MenuItem itemExit = new MenuItem("Exit");
           MenuItem itemcopy = new MenuItem("Copy");
  class MenuJavaExample
          public static void main(String args[])
              MenuExample frame = new MenuExample();
              frame.setTitle("Menu in Java Example");

Menu in Java Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles