Open In App

Java Swing | JPopupMenu

Last Updated : 15 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

JPopupMenu is a class of javax.swing package . It is an implementation of PopupMenu . JPopupMenu generates a small window that pops up and displays a series of choices. JPopupMenu can be used to generate a small window at any position within the container. 
Constructor of the class are : 
 

  1. JPopupMenu() : creates a popup menu with empty name 
     
  2. JPopupMenu(String name) : creates a popup menu with specified title.
     

Commonly used methods of JPopupMenu are : 
 

  1. add(JMenuItem menuItem) : add menuItem to the popup menu.
     
  2. add(String s) : add String to the popup menu .
     
  3. String getLabel() : get the popup menu’s label.
     
  4. boolean is Visible() : returns whether the JPopup menu is visible or not.
     
  5. setLabel(String s) : sets the popup menu’s label.
     
  6. setLocation(int x, int y) : sets the pop up menu’s location to the given coordinates
     
  7. setPopupSize(int width, int height) : set the popup size to give height and width
     
  8. setVisible(boolean b) : sets the visibility of the pop up menu, visible if true is passed as argument or vice versa.
     
  9. show(Component c, int x, int y) : displays the popup menu at x, y position within the component c. 
     

1. Program to show a simple popup menu 
 

Java




// Java program to show a simple popup menu
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Popup extends JFrame implements ActionListener {
 
    // java button
    static JButton b;
 
    // java frame
    static JFrame f;
 
    // popup menu
    static JPopupMenu pm;
 
    // default constructor
    Popup()
    {
    }
 
    // main class
    public static void main(String[] args)
    {
        // create a frame
        f = new JFrame("Popup");
 
        // set the size of the frame
        f.setSize(400, 400);
 
        // close the frame when close button is pressed
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        // create anew panel
        JPanel p = new JPanel();
 
        // create an object of mouse class
        Popup pop = new Popup();
 
        // create a button
        b = new JButton("click");
 
        // addActionListener
        b.addActionListener(pop);
        p.add(b);
        f.add(p);
        f.show();
    }
 
    // when the button is clicked
    public void actionPerformed(ActionEvent e)
    {
        String s = e.getActionCommand();
        if (s.equals("click")) {
            // create a popup menu
            pm = new JPopupMenu("Message");
 
            // create a label
            JLabel l = new JLabel("this is the popup menu");
 
            // add the label to the popup
            pm.add(l);
 
            // add the popup to the frame
            pm.show(f, 100, 100);
        }
    }
}


Output :
 

2.Program to show a popup menu and menu items to it 
 

Java




// Java program to show a popup menu
// and menu items to it
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Popup extends JFrame implements ActionListener {
 
    // java button
    static JButton b;
 
    // java frame
    static JFrame f;
 
    // popup menu
    static JPopupMenu pm;
 
    // JLabel
    JLabel l;
 
    // default constructor
    Popup()
    {
    }
 
    // main class
    public static void main(String[] args)
    {
        // create a frame
        f = new JFrame("Popup");
 
        // set the size of the frame
        f.setSize(400, 400);
 
        // close the frame when close button is pressed
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        // create anew panel
        JPanel p = new JPanel();
 
        // create an object of mouse class
        Popup pop = new Popup();
 
        // create a button
        b = new JButton("click");
 
        // addActionListener
        b.addActionListener(pop);
 
        // create a popup menu
        pm = new JPopupMenu("Message");
 
        // create menuItems
        JMenuItem m1 = new JMenuItem("Item1");
        JMenuItem m2 = new JMenuItem("Item2");
        JMenuItem m3 = new JMenuItem("Item3");
 
        // create a Jlabel
        JLabel l = new JLabel("nothing clicked");
 
        // add menuitems to popup menu
        pm.add(m1);
        pm.add(m2);
        pm.add(m3);
 
        // addActionListener
        m1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                l.setText("Item 1 clicked.");
            }
        });
 
        m2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                l.setText("Item 2 clicked.");
            }
        });
 
        m3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                l.setText("Item 3 clicked.");
            }
        });
 
        // add button and label to frame
        p.add(b);
        p.add(l);
 
        f.add(p);
        f.show();
    }
 
    // when the button is clicked
    public void actionPerformed(ActionEvent e)
    {
        String s = e.getActionCommand();
        if (s.equals("click")) {
 
            // add the popup to the frame
            pm.show(f, 200, 200);
        }
    }
}


Output : 
 

 

Note : This codes might not run in a online IDE please use an offline compiler
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads