Create MenuBar, Menu Items, Menus Java Example

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.

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 Reply

Your email address will not be published. Required fields are marked *