Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

AWT PopupMenu – Show on Right-Click

1. Introduction to AWT PopupMenu

In our past examples, we equipped the AWT Menu in the menu-bar. But we can also display the Menu wherever we need inside a top-level container. The Top-Level container can be a Frame, Dialog or even a Java Applet. Now, have a look at the below picture:


Here, we can see a AWT Menu with three MenuItems in it. We call this as a Pop-up Menu as it is directly owned by the top-level container. Here, in our case, it is an AWT Frame Window. The show method of the AWT PopupMenu class will take three parameters. First one is the parent which owns the Menu. The parent can be a Frame, a Dialog or an Applet. So just like how we add standard components like TextField, Label, CheckBox to the Top-level window, we can add the PopupMenu also to its parent. Second and third arguments tell where we want to display the Menu along with its menu items.

2. About the Example

Below is the PupupMenu we will create in this example:

Java AWT PopupMenu Example
Java AWT PopupMenu Example

Here, we have a PopupMenu with 4 MenuItems and one separator. We can also notice that the Menu is not attached to a Menubar, and it is displayed in the AWT Frame Window. In this example, we will display the Menu when you right click the mouse inside the AWT Frame.

3. Prepare MenuItems for PopupMenu

Our example requires four MenuItem instances in the PopupMenu. Hence, we create all these four menu items and code is below:

In our example, we need to change the background colour when the user clicks on the menu item. So, we will tie each MenuItem with an action command via setActionCommand method.

4. Create The AWT PopupMenu

The PopupMenu class of Java AWT is useful for creating the pop-up menus. Like other standard AWT component, we can add this class to the container class via the add method. In the below code, we store the AWT PopupMenu instance in the reference, popup. Then, we call the add method of the PopupMenu to add the Menu Items to it. Note, at line 6, we also added a separator to our pop-up menu. 

5. AWT PopupMenu & MouseListener

We will display the PopupMenu in the AWT Frame Window’s location where the user clicks the right mouse button. So, our frame window implements MouseListener to track the mouse click.

Since the frame class claims that it will serve handler for listening the MouseEvents, we sign up our AWT Frame with the MouseListener using addMouseListener method. Also note the add method call which adds the PopupMenu as a component to the Frame Window. We require this as we need to show the pop-up menu when user right clicks on the Frame window area.

6. Show Pop-up Menu

To display the pop-up menu, we should call the show method of the PopupMenu instance. This method requires three parameters. They are:

  1. Parent. We pass this as a first argument and in our case, we pass ‘this’ reference as Frame is the parent window of the pop-up menu.
  2. X Co-ordinate value which we can get from the MouseEvent e by calling getX method.
  3. Y Co-ordinate value which we can get from the MouseEvent e by calling getY method.

In the below code, first we check user clicked a right mouse button by checking the constant BUTTON3 with the getButton method call’s return value. Then, we display the pop-up menu at the mouse cursor location by calling show method of the AWT PopupMenu instance.

7. Handle Popup Menu Item Events

To handle the click event of menu items present in the pop-up menu, the frame window implements the ActionListener. The code is below:

Next, all the MenuItems of our pop-up menu enroll with the ActionListener which is nothing but our frame window. After the call to addActionListener, all four menu items will route the ActionEvent to our Frame Window.

Next, we override the actionPerformed handler for our AWT Frame derived class. From ActionEvent e, we get the action command by calling the getActionCommand and form a switch…case based on the outcome. The case body will set the correct background color for the Frame window by making use the setBackground method call.

8. AWT PopupMenu Explained – Youtube Video

9. Code Reference

Categories: AWT

Tags: , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.