Java JPopupMenu

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java JPopupMenu

PopupMenu can be dynamically popped up at specific position within a component. It inherits the JComponent class.

JPopupMenu class declaration

Let's see the declaration for javax.swing.JPopupMenu class.

  1. public class JPopupMenu extends JComponent implements Accessible, MenuElement  

Commonly used Constructors:

Constructor

Description

JPopupMenu()

Constructs a JPopupMenu without an "invoker".

JPopupMenu(String label)

Constructs a JPopupMenu with the specified title.

Java JPopupMenu Example

  1. import javax.swing.*;  
  2. import java.awt.event.*;  
  3. class PopupMenuExample  
  4. {  
  5.      PopupMenuExample(){  
  6.          final JFrame f= new JFrame("PopupMenu Example");  
  7.          final JPopupMenu popupmenu = new JPopupMenu("Edit");   
  8.          JMenuItem cut = new JMenuItem("Cut");  
  9.          JMenuItem copy = new JMenuItem("Copy");  
  10.          JMenuItem paste = new JMenuItem("Paste");  
  11.          popupmenu.add(cut); popupmenu.add(copy); popupmenu.add(paste);        
  12.          f.addMouseListener(new MouseAdapter() {  
  13.             public void mouseClicked(MouseEvent e) {              
  14.                 popupmenu.show(f , e.getX(), e.getY());  
  15.             }                 
  16.          });  
  17.          f.add(popupmenu);   
  18.          f.setSize(300,300);  
  19.          f.setLayout(null);  
  20.          f.setVisible(true);  
  21.      }  
  22. public static void main(String args[])  
  23. {  
  24.         new PopupMenuExample();  
  25. }}  

Java JPopupMenu Example with MouseListener and ActionListener

  1. import javax.swing.*;  
  2. import java.awt.event.*;  
  3. class PopupMenuExample   
  4. {  
  5.      PopupMenuExample(){  
  6.          final JFrame f= new JFrame("PopupMenu Example");  
  7.          final JLabel label = new JLabel();          
  8.          label.setHorizontalAlignment(JLabel.CENTER);  
  9.          label.setSize(400,100);  
  10.          final JPopupMenu popupmenu = new JPopupMenu("Edit");   
  11.          JMenuItem cut = new JMenuItem("Cut");  
  12.          JMenuItem copy = new JMenuItem("Copy");  
  13.          JMenuItem paste = new JMenuItem("Paste");  
  14.          popupmenu.add(cut); popupmenu.add(copy); popupmenu.add(paste);        
  15.          f.addMouseListener(new MouseAdapter() {  
  16.             public void mouseClicked(MouseEvent e) {              
  17.                 popupmenu.show(f , e.getX(), e.getY());  
  18.             }                 
  19.          });  
  20.         cut.addActionListener(new ActionListener(){  
  21.          public void actionPerformed(ActionEvent e) {              
  22.              label.setText("cut MenuItem clicked.");  
  23.          }  
  24.         });  
  25.         copy.addActionListener(new ActionListener(){  
  26.             public void actionPerformed(ActionEvent e) {              
  27.                 label.setText("copy MenuItem clicked.");  
  28.             }  
  29.            });  
  30.         paste.addActionListener(new ActionListener(){  
  31.             public void actionPerformed(ActionEvent e) {              
  32.                 label.setText("paste MenuItem clicked.");  
  33.             }  
  34.            });  
  35.          f.add(label); f.add(popupmenu);   
  36.          f.setSize(400,400);  
  37.          f.setLayout(null);  
  38.          f.setVisible(true);  
  39.      }  
  40. public static void main(String args[])  
  41. {  
  42.         new PopupMenuExample();  
  43. }  
  44. }