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.
//Sample 03: Add menu Bar
MenuBar AwtMenuBar = new MenuBar();
//Sample 04: Create Menus
Menu mnuHelp = new Menu("Help");
Menu mnuDraw = new Menu("Draw");
//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
Now, we we will create the AWT Menu called Action. We add the below code to the Snippet 04 of our previous example:
//Sample 04: Create Menus
//Sample 10: For Sub Menu Demo =>
Menu mnuAction = new Menu("Action");
Next, we add our existing Menus
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
//Sample 11: Add Menu Draw, Help to Action Menu. Previous the
//the owner was the Menubar=>
You can watch the code change in the YouTube video given in the next section.
4. AWT Sub-Menu as Youtube Video
Tags: AWT Menu, AWT MenuBar, AWT MenuItem, AWT Sub-Menu
Do you like this Example? Please comment about it for others!!