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.
public class SimpleMenuExample extends Frame implements ActionListener
Menu states, cities;
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("West Bengal"));
states.addSeparator(); // separates from north Indian states from south Indian
states.add(new MenuItem("Andhra Pradesh"));
cities.addSeparator(); // separates north Indian cities from south Indian
setTitle("Simple Menu Program"); // frame creation methods
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)
A big menu program with code explanation, menu hierarchy and screenshot is available at Java AWT Menu – Multiple Pull-down Lists