Open In App

Java AWT PopupMenu

Last Updated : 13 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, AWT (Abstract Window Toolkit) provides a PopupMenu course that allows you to make pop-up menus in your graphical user port (GUI) applications. A PopupMenu is a setting menu that appears when you right-click on a component, such as a button or a venire. It provides a set of options or actions that the user can select from. In this clause, we testament explore the syntax, methods, and constructors of the Java AWT PopupMenu.

Syntax of Class Declaration

The class declaration for PopupMenu in Java AWT is as follows:

public class PopupMenu extends Menu

Here, PopupMenu is a subclass of the Menu class. It inherits properties and methods from the Menu class and adds functionality specific to pop-up menus.

Methods for PopupMenu

List of all methods used in the PopupMenu application with description

Methods

Description

add(MenuItem m)

Adds the specified MenuItem to this PopupMenu.

show(Component origin, int x, int y)

Displays the pop-up menu at the specified location (x, y) relative to the given component (origin).

remove(int index)

Removes the MenuItem at the specified index from the PopupMenu.

remove(MenuItem m)

Removes the specified MenuItem from the PopupMenu.

removeAll()

Removes all the items from the PopupMenu.

Constructors

The PopupMenu class has a default constructor that can be used to create a PopupMenu:

PopupMenu popupMenu = new PopupMenu();

Example of Java AWT PopupMenu

Below is the implementation of Java AWT PopupMenu:

Java




// Java Program to implement
// Java AWT PopupMenu
import java.awt.*;
import java.awt.event.*;
  
// Driver Class
public class PopupMenuExample {
      // main function
    public static void main(String[] args) {
        
        Frame frame = new Frame("GeeksforGeeks PopupMenu Example");
        frame.setSize(400, 400);
  
        // Create a PopupMenu
        PopupMenu popupMenu = new PopupMenu();
  
        // Create MenuItems
        MenuItem menuItem1 = new MenuItem("Option 1");
        MenuItem menuItem2 = new MenuItem("Option 2");
        MenuItem menuItem3 = new MenuItem("Option 3");
  
        // Add MenuItems to the PopupMenu
        popupMenu.add(menuItem1);
        popupMenu.add(menuItem2);
        popupMenu.add(menuItem3);
  
        // Add the PopupMenu to the Frame
        frame.add(popupMenu);
  
        // Add a MouseListener to the Frame to display the PopupMenu
        frame.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popupMenu.show(frame, e.getX(), e.getY());
                }
            }
  
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popupMenu.show(frame, e.getX(), e.getY());
                }
            }
        });
  
        frame.setLayout(null);
        frame.setVisible(true);
        
    }
}


Output:

Video Demo of the Example of AWT PopupMenu

Output Window:

Output Window for AWT PopupMenu

Conclusion

The Java AWT PopupMenu family is a utile factor for creating context menus in your GUI applications. It provides a way to represent options or actions to the user in a pop-up form. By knowing its syntax, methods, and constructors, you can enhance the interactivity of your Java applications.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads