Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Popup Menus

The JPopupMenu class is the functional equivalent class to the JMenuBar class for popup menus; typical of the Java programming language, it works the same way as its counterpart. You can add JMenu and JMenuItem objects to a JPopupMenu object by using add() and insert() methods (with the main difference being how the menu is displayed). In the JMenuBar class, we set the class as the container’s menu bar, but with the JPopupMenu class we are going to display it by asking it to display itself at a specified location on the screen.

Tables 11 and 12 show the constructors and useful methods of the JPopupMenu classes, respectively.

Table 11. JPopupMenu Constructors

Constructor

Description

JPopupMenu()

Creates a JPopupMenu without an "invoker"

JPopupMenu(String label)

Creates a JPopupMenu with the specified title

Table 12. JPopMenu Methods

Method

Description

JMenuItem add(Action)

Appends a new menu item to the end of the menu, which dispatches the specified Action object

JMenuItem add(JMenuItem)

Appends the specified menu item to the end of this menu

JMenuItem add(String)

Creates a new menu item with the specified text and appends it to the end of this menu

void addSeparator()

Appends a new separator at the end of the menu

void insert(Action, int index)

Inserts a menu item for the specified Action object at a given position

void insert(Component, int index)

Inserts the specified component into the menu at a given position

void show(Component invoker, int x, int y)

Displays the popup menu at the position x, y in the coordinate space of the component invoker

The add, addSeparator, and insert methods are similar to those in the JMenuBar class, but the show method is new. The show method displays the popup menu at the (x, y) location you specify; the invoker component is the component in whose space the popup menu should appear. Typically, you will listen for mouse events and display the JPopupMenu when the user clicks the right mouse button (context menu).

Before we leave the topic of JPopupMenu class, it is worth mentioning that the JMenu class actually contains a JPopupMenu object, and that object appears when it is selected. For example, when you click the “File” menu, the JMenuItem objects displayed are shown in a JPopupMenu; the JPopupMenu class provides an interface for applications and applets to display the menu at any location.

The following listing shows the construction and presentation of a JPopupMenu that you might see when selecting text in a word processing document.

// Create the menu items
JMenuItem editCut = new JMenuItem( “Cut” );
JMenuItem editCopy = new JMenuItem( “Copy” );
JMenuItem editPaste = new JMenuItem( “Paste” );
JMenuItem editDelete = new JMenuItem( “Delete” );

// Create the popup menu
JPopupMenu popupMenu = new JPopupMenu();

// Add the menu items to the popup menu
popupMenu.add( editCut );
popupMenu.add( editCopy );
popupMenu.add( editPaste );
popupMenu.addSeparator();
popupMenu.add( editDelete );

// Display the popup menu at location (100, 100)
popupMenu.show( this, 100, 100 );

Listing 1.5 creates a popup menu with four menu items and displays it in the current class’s display area at location (100, 100). You will see this action in a few articles, when we get to editing text in Jword. But experiment with it yourself for now.

  • + Share This
  • 🔖 Save To Your Account