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:
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:

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:

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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
//Sample 03: Add menu Bar MenuBar AwtMenuBar = new MenuBar(); setMenuBar(AwtMenuBar); //Sample 04: Create Menus Menu mnuHelp = new Menu("Help"); Menu mnuDraw = new Menu("Draw"); AwtMenuBar.add(mnuDraw); AwtMenuBar.add(mnuHelp); //Sample 05: Create Menu Items MenuItem siLine = new MenuItem("Line"); MenuItem miLine = new MenuItem("Multi Line"); MenuItem Rect = new MenuItem("Rectangle"); MenuItem Pentagon = new MenuItem("Pentagon"); MenuItem Circle = new MenuItem("Circle"); MenuItem help = new MenuItem("F1 Help"); MenuItem about = new MenuItem("About Menu Demo"); //Sample 06: Add Menu Items to Menu mnuDraw.add(siLine); mnuDraw.add(miLine); mnuDraw.add(Rect); mnuDraw.add(Pentagon); mnuDraw.add(Circle); mnuHelp.add(help); mnuHelp.add(about); |
Now, we we will create the AWT Menu called Action. We add the below code to the Snippet 04 of our previous example:
1 2 3 4 |
//Sample 04: Create Menus //Sample 10: For Sub Menu Demo => Menu mnuAction = new Menu("Action"); //<= 10 |
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.
1 2 3 4 5 6 |
//Sample 11: Add Menu Draw, Help to Action Menu. Previous the //the owner was the Menubar=> mnuAction.add(mnuDraw); mnuAction.add(mnuHelp); AwtMenuBar.add(mnuAction); //<= 11 |
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: AWT Menu, AWT MenuBar, AWT MenuItem, AWT Sub-Menu