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 Sub-Menu Example

1. Introduction to AWT Sub-Menu

In the last two examples, we learned how to create AWT MenuItems and CheckboxMenuItems. In this example, we will learn how to create sub-menu. Have a look at the below picture:

Java AWT Sub-Menu & Menu
Java AWT Sub-Menu & Menu

Here, AWT opens a pull-down menu when the user clicks the File Menu Title in the Menubar. Pull-down Menu is the instance of an AWT Menu. The AWT Menu can act as a parent for MenuItems like New, Open and Save. It can also be a parent for other AWT Menu like Import. When the user clicks the Menu Title Import, it opens an AWT Pull-Down menu, which we call as a Sub-Menu. So, in the above picture, both File and Import are AWT Menu instances. We call Import as a sub-menu for the File menu since the parent of AWT Menu instance Import is also a Menu. Note, the owner of the File menu is a Menubar.

2. Quick Review of Past Example

In the past example on AWT Menu Basic, we created a simple menu using Menubar, Menu and Menu items. The AWT Frame windows looked as follows:

AWT MenuItem & Menu Example
AWT MenuItem & Menu Example

In this example, we will create a new Menu called Action and handover that to the Menu bar. In the above picture, the menu bar owns the Draw and Help menus. For this example, we will add these two menus to the Action menu. Doing so will change these two menus as sub-menus of the action menu.

3. Change Draw & Help as AWT Sub-Menus

The below picture shows the example we will create here:

Java SWT Sub-Menu Example
Java SWT Sub-Menu Example

Below is the code from our previous example of simple AWT Menu. Code snippet 4 shows that the Menus Draw and Help are child of the Menubar. Now we need to shift this ownership to action menu.

Now, we we will create the AWT Menu called Action. We add the below code to the Snippet 04 of our previous example:

Next, we add our existing Menus mnuDraw and mnuHelp to the Action menu, which we created above. Now, these two menus are the sub-menus for the action menu. Finally, we add the Action menu to the Menubar using the add method.

You can watch the code change in the YouTube video given in the next section.

4. AWT Sub-Menu as 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.