Wednesday, 12 March 2014


Java's Abstract Windowing Toolkit (AWT) includes four concrete menu classes: Menu, MenuItem ,MenuBar ,CheckboxMenuItem......


Java's Abstract Windowing Toolkit (AWT) includes four concrete menu classes: menu bars representing a group of menus that appears across the top of the window or the screen (class MenuBar); pull-down menus that pull from menu bars or from other menus (class Menu); menu items that represent menu selections (class MenuItem); and menu items that the user can turn on and off (class CheckBoxMenuItem).
                  These classes are all subclasses of MenuComponent, not subclasses of Component. Because they aren't components, they can't be placed in any container -- the way you would place buttons and lists in a container. In a graphical user interface (GUI) the only way to use these menu classes is to place a menu bar (which can contain additional menus) in a frame using the frame's setMenuBar method. Since the applet class is not a subclass of Frame, it does not inherit the setMenuBar method. 

This means you cannot simply place a menu bar in an applet.

The main advantages of using AWT menus in applets rather than using a custom pop-up-menu class are:

  • Using a custom pop-up-menu class requires you to either develop the class, purchase it, or at least find a suitable free one, whereas the AWT menus are built into every Java run-time environment.

  • The AWT menus always have the look and feel of the native windowing environment, whereas a custom pop-up-menu widget might not.

  • An applet that uses the AWT menus loads faster because it does not need to load the menu class from across the network.

Following are the constructers for menu:

  • Menu()
  • Menu(string optionName)
  • Menu(String optionName,boolean removable)

 Here,optionName specifies the name of the menu selection.If removable is true the pop-up menu can be removed  and allowed to float free.Other wise it will attached to the menu bar.

Individual menu items are of type MenuItem.It defines these constructer:

  • MenuItem()
  • MenuItem(String itemName)
  • MenuItem(String itemName,MenuShortcut keyAccel)

 Here,itemName is the name shown in the menu,and keyAccel is the menu shortcut for this item.

 you can create a checkable menu item by using a subclass of MenuItem called CheckboxMenuItem.It has these constructors

  • CheckboxMenuItem()
  • CheckboxMenuItem(String itemName)
  • CheckboxMenuItem(String itemName,boolean on)

Checkable items operate as toggles.Each time one is seelected, its state the first two forms,the checkable entry is unchecked.In the third form , if on is true,the checkable entry is initially checked.otherwise it is cleared


  • seEnabled() method:  If the argument  enabledFlag is true ,the menu item is enabled.if false,the menu item is disabled.
                            void setEnabled(bollean enabledFlag):

  • isEnabled() method: To determine an item's status by calling isEnabled().

                     boolean isEnabled()
  • add() method: Once you  have created a menu item,you must add the item to a menu object by using add().

                    MenuItem add(MenuItem item)
   here item is being added.Once you have added all items to a menu object,you can add that object to the menu bar by using this version of add() defined by MenuBar.

                   Menu add(Menu menu)
 here,menu is the menu being added.

sample program

import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class AddingRemovingJMenu
 public static void main(final String args[])
JFrame frame = new JFrame("MenuSample Example");
JMenuBar menuBar = new JMenuBar();
    // File Menu, F - Mnemonic
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
frame.setSize(350, 250);


No comments :

Post a Comment