Java AWT Menu

Java AWT Menu

Menus are very familiar to a programmer in Windows environment. A menu comes with a pull-down list of menu items from which user can select one at a time. When a lot of options in different categories exist to be opted by the user, menus are the best choice as they take less space on the frame. A click on the MenuItem generates ActionEvent and is handled by ActionListener. Java AWT Menu and MenuItem are not components as they are not subclasses of java.awt.Component class. They are derived from MenuComponent class. Following hierarchy illustrates.

Java AWT Menu Hierarchy

Menus creation involves lot of classes like MenuBar, Menu and MenuItem and one is added to the other.

Java AWT Menu

MenuComponent is the super most class of all menu classes; like Component is the super most class for all component classes like Button, Frame etc. MenuBar is capable to hold the menus and Menu can hold menu items. Menus are placed on menu bar.

Steps of Creating Java AWT Menu

Creation of menus involves many steps to be followed in an order. Following are the steps.

  1. Create menu bar
  2. Add menu bar to the frame
  3. Create menus
  4. Add menus to menu bar
  5. Create menu items
  6. Add menu items to menus
  7. Event handling

In the following program, three menus are created and populated with menu items. User's selected menu item information is displayed in the text area.

Example on Java AWT Menu

Java AWT Menu

4 thoughts on “Java AWT Menu

Leave a Reply

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