Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

AWT MenuItem and Menu – Example

1. Introduction to AWT MenuItem and Menu

Most of know about Menus and AWT Menu also same with the same standards. When we talk about Menu, we must know about Menubar and AWT Menuitem. The below picture shows the relation between Menus, Menubar and Menu Items.

Menubar Menu and Menuitem Relation
Menubar Menu and Menuitem Relation

In the above picture, we can see a Frame Window is housing the Menubar. The same way a dialog also can show a Menubar. A Menubar can hold one or more menus. Here, the picture shows three menus named File, Edit, and Insert. Our Menubar can show only the Menu name in it. But when the user clicks it, it opens the pop-up Menu which contains one or more Menu Items. When the user clicks a menu item, the MenuItem will produce the ActionEvent which can be handled using the ActionListener.

2. About AWT MenuItem Example

In this example, we will create two menus with title Draw and Help. The example is shown below:

AWT MenuItem & Menu Example
AWT MenuItem & Menu Example

The Draw menu contains Five menu items. In the Help menu, we will add two menu items which are not shown in the above picture. We will also handle the click event of the menu items and display which menu is clicked at the bottom of the Frame Window.

3. Add Status Label

The class FrameWin is sub-class of the AWT Frame, and we have a Label control lblDisplay as a member. This label will display which menu items user pressed. Note, we add this label towards the south of the Frame window. Below is the code:

4. Setup AWT MenuItem, Menubar & Menu

Now our frame window has a status label on the bottom to report the selected menu items. In this section, we will create Menubar, Menu and Menu Items components.

4.1 Setup MenuBar

Menubar class in Java AWT is the base for creating menu and menu items. So, we create this instance first and add it to the top-level window. In our case, we add it to the Frame by calling its method setMenuBar.

4.2 Setup AWT Menu

The Java AWT Menu class will display a pull-down menu. We call it pull-down because the menu will get displayed when we click on it. One can attach the Menu to the Menubar via the add method. For our example, we create two Menu objects and add it to the Menubar.

After adding the above code, the Menubar displays the Menus Draw and Help. But, when we click this menu, nothing happens because there is no menu item to show right now.

4.3 Setup AWT MenuItem

The MenuItem class represents the menu item of a menu. This menu item will show a command to the user. When you click menu, it displays the menu items. In the below code, we create seven menu items. First five menu items are for the Draw Menu and second two menu items are for the Help Menu. Each constructor takes a string, and it will be displayed as a label for the Java AWT MenuItem. Below is the code:

4.4 Link Menu & AWT MenuItem

The add method of the Menu class takes a menu item as a param and adds it. One can add a multiple number of menu items to a menu using the add method. In the below code, we added the menu items to the Draw and Help menu.

Now the complete hierarchy is:

  1. The Frame Window contains the Menu bar.
  2. Menu bar contains two menus Draw and Help.
  3. Both the menus Draw, and Help contains some menu items in it.

6. Setup Action Commands

The action commands are useful to know the event originator when all the components route the event to a same handler function. Here, we set action command for all our menu items via setActionCommand method.

After setting the action command, we register the AWT MenuItems with the action listener by calling the method addActionListener. Since we pass this object to the method, we will provide the actionPerformed handler here in this AWT Frame class itself.

7. Display Clicked MenuItem Name

In the actionPerformed handler, we retrieve the action command from the ActionEvent object e. Then, pass the return value to the setText method of the Label control which is sitting in the south of the Frame Window.

8. Watch Java AWT Menu – Youtube Video

Categories: AWT

Tags: , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.