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.
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:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class FrameWin extends Frame implements WindowListener, ActionListener { //Sample 01: Class Members Label lblDisplay; public FrameWin(String FrameTitle) { //Display the Frame Window super(FrameTitle); setSize(400, 400); setLocation(100,100); addWindowListener(this); //Sample 02: Add a Label Control lblDisplay = new Label(); lblDisplay.setBackground(Color.LIGHT_GRAY); add(BorderLayout.SOUTH, lblDisplay); |
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.
1 2 3 |
//Sample 03: Add menu Bar MenuBar AwtMenuBar = new MenuBar(); setMenuBar(AwtMenuBar); |
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.
1 2 3 4 5 |
//Sample 04: Create Menus Menu mnuHelp = new Menu("Help"); Menu mnuDraw = new Menu("Draw"); AwtMenuBar.add(mnuDraw); AwtMenuBar.add(mnuHelp); |
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:
1 2 3 4 5 6 7 8 |
//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"); |
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.
1 2 3 4 5 6 7 8 |
//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 the complete hierarchy is:
- The Frame Window contains the Menu bar.
- Menu bar contains two menus Draw and Help.
- 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.
1 2 3 4 5 6 7 8 |
//Sample 07: Set ActionCommand & register with Listeners siLine.setActionCommand("Draw Line"); miLine.setActionCommand("Draw Multi Line"); Rect.setActionCommand("Draw Rectangle"); Pentagon.setActionCommand("Draw Pentagon"); Circle.setActionCommand("Draw Circle"); help.setActionCommand("F1 Help"); about.setActionCommand("About Menu Demo"); |
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.
1 2 3 4 5 6 7 8 |
//Sample 08: Add Action Listener siLine.addActionListener(this); miLine.addActionListener(this); Rect.addActionListener(this); Pentagon.addActionListener(this); Circle.addActionListener(this); help.addActionListener(this); about.addActionListener(this); |
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.
1 2 3 4 5 |
@Override public void actionPerformed(ActionEvent ae) { //Sample 09: Set the Label lblDisplay.setText(ae.getActionCommand()); } |
8. Watch Java AWT Menu – Youtube Video
Categories: AWT
Tags: AWT Menu, AWT MenuBar, AWT MenuItem, setMenuBar