Turn Desktop View Off
by Dinesh Thakur

A popupmenu is a small window that pops up and displays a list of choices such as Cut, Copy, Paste, Select All etc. This menu is invisible until the user makes a platform specific mouse action such as pressing the right mouse button over a popup enabled component. The popup menu then appears under the cursor.

In order to create a pop up menu, java .awt .PopupMenu class is used. It is a subclass of Menu and is used very much like a Menu. It provides the following two constructors

• PopupMenu () - Creates an empty popup menu.

• PopupMenu (String title) - Creates an empty pop up menu with the as the title of the popup menu.

As expected, the AWT provides fairly complete support for creating applications using the menu bar (menu bar), drop down menus (pull-down menus) and  independent menu (pop-up menus) through set of classes briefly described following:

class

Description

MenuComponent

Abstract class that defines the behavior and structure Basic elements of the components menu.

MenuItem

class that defines a simple menu entry.

MenuBar

class that defines a menu bar for an application.

Menu

Class that defines a menu dropdown.

MenuShortCut

class that defines a shortcut to a menu item.

CheckboxMenuItem

class that defines a menu entry type option.

PopupMenu

class that defines an independent menu.

import java.awt.*;

class PopupMenuExample extends Frame
{
     PopupMenu popupMenu;
     public PopupMenuExample()
    {
           setLayout(new FlowLayout());
           popupMenu = new PopupMenu();
           popupMenu.add(new MenuItem("Cut"));
           popupMenu.addSeparator();
           popupMenu.add(new MenuItem("Cut"));
           popupMenu.addSeparator();
           popupMenu.add(new MenuItem("paste"));
           add(popupMenu);   
           setTitle("PopupMenu in Java Example");
           setSize(300,120);
           setVisible(true);
           popupMenu.show(this,100,100);
    }
}
 class PopupMenuJavaExample
 {
        public static void main(String[] args)
       {
            PopupMenuExample frame = new PopupMenuExample();
       }
 }

PopupMenu in Java Example