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 :
- JPopupMenu() : creates a popup menu with empty name
- JPopupMenu(String name) : creates a popup menu with specified title.
Commonly used methods of JPopupMenu are :
- add(JMenuItem menuItem) : add menuItem to the popup menu.
- add(String s) : add String to the popup menu .
- String getLabel() : get the popup menu’s label.
- boolean is Visible() : returns whether the JPopup menu is visible or not.
- setLabel(String s) : sets the popup menu’s label.
- setLocation(int x, int y) : sets the pop up menu;s location to the given coordinates
- setPopupSize(int width, int height) : set the popup size to give height and width
- setVisible(boolean b) : sets the visibility of the pop up menu, visible if true is passed as argument or vice versa.
- 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 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 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Java Swing | JTextField
- Java Swing | JTextArea
- Java Swing | JSpinner
- Java Math decrementExact() method
- Java Math incrementExact() method
- Java Math negateExact() method
- Java lang.Integer.toBinaryString() method
- Java Swing | JPasswordField
- Java Swing | JProgressBar
- Draw a Polygon in Java Applet
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.


