0% found this document useful (0 votes)
52 views

Java JPopupMenu

The document discusses the JPopupMenu class in Java Swing. JPopupMenu allows dynamically displaying a menu at a specified location on a component. It inherits from JComponent. Constructors can create a JPopupMenu without an invoker or with a specified title string. The document also provides an example of using JPopupMenu with a mouse listener to display the menu and action listeners to handle menu item clicks.

Uploaded by

Omondi Edwin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Java JPopupMenu

The document discusses the JPopupMenu class in Java Swing. JPopupMenu allows dynamically displaying a menu at a specified location on a component. It inherits from JComponent. Constructors can create a JPopupMenu without an invoker or with a specified title string. The document also provides an example of using JPopupMenu with a mouse listener to display the menu and action listeners to handle menu item clicks.

Uploaded by

Omondi Edwin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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. }}  

Output:

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. }  

Output:

You might also like