Menu, MenuBar and Dialog -


Menu Bars and Menus -

A window in awt can have a menu bar that have a list of menus from which user can select.
Each menu again has a list menu items that are displayed in a drop down fashion.
AWT provides the following classes to implement this concept -
MenuBar, Menu, MenuItem, CheckboxMenuItem
To use the concept of menu and menu bar you need to follow the following steps -
1- First of all create an object of MenuBar that will contain all the menus. MenuBar has only one constructor -
public MenuBar()  
It create a new menu bar.
Example -
MenuBar mb = new MenuBar();
2- Create menus within menu bar by creating the objects of Menu class. Menu are the top level options displayed in menu bar. Menu has the following constructors -
public Menu()
It creates a menu with no label.
public Menu(String label)
It create a menu with the specified label name.
public Menu(String label, boolean removable) 
It also create a menu with the specified label name. if removable is true, menu can be removed and allowed 
to float free.
You can use any of the constructors to create objects of Menu.
Example -
Menu m1 = new Menu("File");
Menu m2 = new Menu("Edit");

3- Create menu items within menus by creating the object of MenuItem class. It has the following constructors -
public MenuItem()
It creates menu item with no label.
public MenuItem(String label)
It creates a menu item with the specified label name.
pubic MenuItem(String label, MenuShortcut s)
It creates a labeled menu with the associated keyboard shortcut. 
You can use any of the constructors to create objects of MenuItem.
Example -

MenuItem mi1 = new MenuItem("New");
MenuItem mi2 = new MenuItem("Open");
MenuItem mi3 = new MenuItem("Save"); 

MenuItem methods - Some useful methods provided by MenuItem that can be used to manipulate menu items are as follows -
  • setEnabled(boolean val)- You can enables or disabled any menu item by using this method, it has the following syntax -
    public void setEnabled(boolean val) 
    if val is true, menu item is enabled , if false menu item is disabled.
    
  • isEnabled()- You can also check whether menu item is enabled or disabled by using this method. It has the following general syntax -
    public boolean isEnabled()   
    
  • setLabel(String label)- It sets the label of menu item to the specified one. It has the following general form -
    public void setLabel(String label)
    where label is the new label of menu item.
    
  • getLabel()- You can also get the label of any menu item by using this method. It has the following general form -
    public String getLabel()
    

4- If you need to create checkbox menu items, you can create them by creating the object of CheckboxMenuItem class it is direct subclass of MenuItem class.
CheckboxMenuItem has the following constructors -
  • public CheckboxMenuItem()
    It creates a checkbox menu item with no label specified.
    
  • public CheckboxMenuItem(String label)
    It creates a checkbox menu item with the specified label. By default checkbox menu item will be off (unchecked).
    
  • public CheckboxMenuItem(String label, boolean state)
    It creates a checkbox menu item with the specified label and state. If the state is true , checkbox menu item 
    will be on (checked) otherwise off (unchecked).
    
  • Example -
    CheckboxMenuItem cmi1 = new CheckboxMenuItem("Status Bar");
    CheckboxMenuItem cmi2 = new CheckboxMenuItem("Document Tabs");
    

Methods of CheckboxMenuItem -
Some useful methods of CheckboxMenuItem are as follows -
  • setState(boolean val) - It sets the checkbox menu item to the specified state. It has the following general form -
    public void setState(boolean val)
    If val is true, checkbox menu item will be on otherwise it will be off.
    
  • getState()- It returns the state of checkbox menu item. It has the following general form -
    public boolean getState()
    

5- Next add the created menu items to the menus. Menu class provides the add() method to add items to menus. It has the following general form -
public MenuItem add(MenuItem item)
It adds and returns the menu item added to menu. If the specified menu item is the part of any other menu,
then it will remove it from that menu.
Example -

m1.add(mi1);
m1.add(mi2);
m1.add(mi3);


6- Finally add the created menus to the menu bar. MenuBar provides the add() method to add menus to menu bar. It has the following general form -
public Menu add(Menu menu)
It adds and returns the menu added to menu bar. If the specified menu item is the part of any other menu, 
then it will remove it from that menu.
Example-

mb.add(m1);
mb.add(m2);

Example -

This example will create the following menu bar -


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends Frame implements ActionListener
{
    MenuBar mb;
    Menu m1, m2, m3, m4;
    MenuItem mi1, mi2,mi3,mi4,mi5,mi6,mi7,mi8, mi9,mi10;
    CheckboxMenuItem cmi1, cmi2;
    
    public Test()
    {
        setSize(400 , 400);
        setTitle("Menu Demo");
        mb = new MenuBar();  // create a menu bar
        setMenuBar(mb);   // add menu bar to frame
        
        // Create Menus
        m1 = new Menu("File");
        m2 = new Menu("Edit");
        m3 = new Menu("View");
        m4 = new Menu("Print");
        // create menu items
        mi1 = new MenuItem("New");
        mi2 = new MenuItem("Open");
        mi3 = new MenuItem("Save");
        mi4 = new MenuItem("Print Preview");
        mi5 = new MenuItem("Page Setup");
        mi6 = new MenuItem("Exit");
        mi7 = new MenuItem("Cut");
        mi8 = new MenuItem("Copy");
        mi9 = new MenuItem("Paste");
        
        cmi1 = new CheckboxMenuItem("Status Bar");
        cmi2 = new CheckboxMenuItem("Document Tabs");
        mi10 = new MenuItem("Full Screen");
        
        // add menus to menu bar
        
        mb.add(m1);
        mb.add(m2);
        mb.add(m3);
        
        // add menu items to different menus
        
        m1.add(mi1);
        m1.add(mi2);
        m1.add(mi3);
        
        // add Print menu within File menu
        m1.add(m4);  
        
        // add Print options i.e. Print Preview and page Setup within Print menu
        m4.add(mi4);
        m4.add(mi5);
        
        m1.add(mi6);
        
        m2.add(mi7);
        m2.add(mi8);
        m2.add(mi9);
        
        m3.add(cmi1);
        m3.add(cmi2);
        m3.add(mi10);
        
        mi6.addActionListener(this);
        
    }
    public void actionPerformed(ActionEvent e)
    {
        System.exit(0);
    }
    
    public static void main(String args[])
    {
        Test t1 = new Test();
        t1.setVisible(true);
    }
}