We can also develop an application with a Menu. As a name indicates a Menu consists of Menu objects.



We can also develop an application with a Menu. As a name indicates a Menu consists of Menu objects. These Menu objects comprise of MenuItem objects which can be selected by the user with a click of a mouse. A MenuItem may be a String, checkbox, separator, menu etc.

Following are the steps to to add menus to any Frame:

  1. You need to create a MenuBar first with the help of the following method.

  MenuBar mb = new MenuBar();

   2.    Then you need to create a Menu using Menu m = new Menu("File");.

   3.  Now the MenuItem options can be added to the Menu from top to bottom, using the following methods.

  mi.add(new MenuItem("Open"));
  mi.add(new CheckboxMenuItem("Type here")); 

  4Now you can add the Menu to the MenuBar from left to right using mi.add(m);.

  5.   Finally, you need to add the MenuBar to the Frame by calling the setMenuBar() method.

The program code given below, creates an application window with a menu bar.


import java.awt.*; 
import java.awt.event.*; 
public class MainWindow extends Frame 
  public MainWindow() { 
  super("Menu Window")
  FileMenu fileMenu = new FileMenu(this)
  HelpMenu helpMenu = new HelpMenu(this)
  MenuBar mb = new MenuBar()
  addWindowListener(new WindowAdapter() { 
  public void windowClosing(WindowEvent e) { 
  public void exit() { 
  public static void main(String args[]) { 
  MainWindow w = new MainWindow()

class FileMenu extends Menu implements ActionListener 
  MainWindow mw;  
  public FileMenu(MainWindow m) { 
  mw = m; 
  MenuItem mi; 
  add(mi = new MenuItem("Open"))
  add(mi = new MenuItem("Close"))
  add(mi = new MenuItem("Exit"))
  public void actionPerformed(ActionEvent e) { 
  String item = e.getActionCommand()
  if (item.equals("Exit"))  
  System.out.println("Selected FileMenu " + item)

class HelpMenu extends Menu implements ActionListener 
  MainWindow mw;  
  public HelpMenu(MainWindow m) { 
  mw = m; 
  MenuItem mi; 
  add(mi = new MenuItem("Basics"))
  add(mi = new MenuItem("Advanced"))
  add(mi = new CheckboxMenuItem("Manual"))
  Menu subMenu = new Menu("Miscellaneous")
  subMenu.add(mi = new MenuItem("Help"))
  subMenu.add(mi = new MenuItem("Other Option"))
  public void actionPerformed(ActionEvent e) { 
  String item = e.getActionCommand()
  if (item.equals("Basics")) 
  else if (item.equals("Help"))  

Output of the program:


C:\newprgrm>java MainWindow

Download this example.


Share on Google+Share on Google+


Posted on: December 18, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Menus   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
May 23, 2011

i'm this prog for many times but still getting errors
January 29, 2012
sample program

how can we have event on the menu item. I can't get this.. plz help. what if we want to add mouse click event in the open menu item and the source for the menu item is another class. how can we import the class and call the methods of the imported class???
deepak kumar
February 21, 2012

we want to menu program in java
mrigendra pratap singh
November 15, 2012
appearence of menu

first of all,thanks for simple programme on menu. but the appearence of menu is simple.