Create MenuBar, Menu Items, Menus Java Example

Menus Java are a number of pull-down combo boxes (In Java called as Choice) placed at single place for easy selection by the user.

To create menus, the java.awt package comes with mainly four classes – MenuBar, Menu, MenuItem and CheckboxMenuItem. All these four classes are not AWT components as they are not subclasses of java.awt.Component class. Infact, they are subclasses of java.awt.MenuComponent which is is no way connected in the hierarchy with Component class.

  • MenuBar: MenuBar holds the menus. MenuBar is added to frame with setMenuBar() method. Implicitly, the menu bar is added to the north (top) of the frame. MenuBar cannot be added to other sides like south and west etc.
  • Menu: Menu holds the menu items. Menu is added to frame with add() method. A sub-menu can be added to Menu.
  • MenuItem: MenuItem displays the actual option user can select. Menu items are added to menu with method addMenuItem(). A dull-colored line can be added in between menu items with addSeparator() method. The dull-colored line groups (or separates from other) menu items with similar functionality like cut, copy and paste.
  • CheckboxMenuItem: It differs from MenuItem in that it appears along with a checkbox. The selection can be done with checkbox selected.
Following is a simple Menus Java program with just two menus added with few menu items. User selection is displayed at DOS prompt for simplicity to get the concept. A big program we see later.
import java.awt.*;
import java.awt.event.*;

public class SimpleMenuExample extends Frame implements ActionListener
  Menu states, cities;
  public SimpleMenuExample()
    MenuBar mb = new MenuBar();		        // begin with creating menu bar
    setMenuBar(mb);				// add menu bar to frame

    states = new Menu("Indian States");		// create menus   
    cities = new Menu("Indian Cities");	

    mb.add(states);				// add menus to menu bar

    states.addActionListener(this);		// link with ActionListener for event handling

    states.add(new MenuItem("Himachal Pradesh"));
    states.add(new MenuItem("Rajasthan"));
    states.add(new MenuItem("West Bengal"));
    states.addSeparator();			// separates from north Indian states from south Indian
    states.add(new MenuItem("Andhra Pradesh"));
    states.add(new MenuItem("Tamilnadu"));
    states.add(new MenuItem("Karnataka"));

    cities.add(new MenuItem("Delhi"));
    cities.add(new MenuItem("Jaipur"));
    cities.add(new MenuItem("Kolkata"));
    cities.addSeparator();			// separates north Indian cities from south Indian
    cities.add(new MenuItem("Hyderabad"));
    cities.add(new MenuItem("Chennai"));
    cities.add(new MenuItem("Bengaluru"));

    setTitle("Simple Menu Program");		// frame creation methods
    setSize(300, 300);
  public void actionPerformed(ActionEvent e)
    String str = e.getActionCommand();		// know the menu item selected by the user
    System.out.println("You selected " + str);
  public static void main(String args[])
    new SimpleMenuExample();

Menus Java

A big menu program with code explanation, menu hierarchy and screenshot is available at Java AWT Menu – Multiple Pull-down Lists

View All for Java Differences on 90 Topics

Leave a Comment

Your email address will not be published.