1. Introduction JPopupMenu
We know a menu contains one or more menu items. We also saw how to display a menu from the Menubar in one of our examples. The menu can be displayed at mouse cursor location as well. Java provides a class called JPopupMenu which one can use to display a context menu at the mouse cursor location. The show method of the JPopupMenu takes (x, y) location and displays the pop-up menu at that location.
2. About the Context Menu Example
We will create an example as shown below:

Our example is a JFrame Window which can display a Context Menu. A context menu will appear at the click location of the mouse when user right clicks anywhere in the form except the title bar. The displayed menu will not have any title and it contains three menu items in it.
3. Create JPopupMenu
First, we create a JFrame which will be the parent for the JPopupMenu. Because the parent will display the context menu, the child will get closed when the user closes the Parent. In our case, JFrame is the parent and Context Menu, which we will display in the coming section is its child. So, when a user closes the Parent, it will close the Context Menu if it is already displaying. Since we want to display the context menu on a mouse right click location, our parent should implement the MouseListener. Below is the code:
1 2 3 4 5 6 7 8 9 10 11 12 |
@SuppressWarnings("serial") //Sample 02: Implement MouseListener public class SwingPopupMenuExample extends JFrame implements MouseListener { public SwingPopupMenuExample(String title) { //Sample 01: Set Size and Position super(title); setBounds(100, 100, 450, 250); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); |
After setting-up the parent (JFrame), we declare a Java Swing JPopupMenu as a class member. Refer code snippet 03 below:
1 2 3 4 5 6 |
//Sample 02: Implement MouseListener public class SwingPopupMenuExample extends JFrame implements MouseListener { //Sample 03: Member Variable JPopupMenu popMnu; |
Code Snippet 04 creates an instance of the JPopupMenu and then adds three sample menu items to it. This is the same as how we prepared JMenu by adding JMenuItem to it. Once context menu is ready, snippet 05 registers the JFrame window to receive mouse events. Because, we need to display our context menu when the user right clicks on the JFrame parent.
1 2 3 4 5 6 7 8 9 10 11 |
//Sample 04: Create Popup Menu popMnu = new JPopupMenu(); JMenuItem miCut = new JMenuItem("Cut"); JMenuItem miCopy = new JMenuItem("Copy"); JMenuItem miPaste = new JMenuItem("Paste"); popMnu.add(miCut); popMnu.add(miCopy); popMnu.add(miPaste); //Sample 05: Add MouseListener addMouseListener(this); |
4. Display JPopupMenu
Our JFrame class implements MouseListener. It provides dummy implementations for all but mousePressed handler. Mouse-Pressed handler checks whether the user clicked the right mouse button or not. When it knows the user clicked the right-mouse button, the handler calls the show method of the JPopupMenu. First param is the parent as displaying JPopupMenu needs as parent. Second and third params tell the (x, y) location for displaying context menu. Below is the handler code for mousePressed:
1 2 3 4 5 6 7 |
@Override public void mousePressed(MouseEvent e) { //Sample 06: Show Popup menu at right click location if (e.getButton() == MouseEvent.BUTTON3) popMnu.show(this, e.getX(), e.getY()); } |
5. Youtube Demo – Display Context Menu
6. Code Reference
6.1 MainEntry.java
1 2 3 4 5 6 7 8 9 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { SwingPopupMenuExample frame = new SwingPopupMenuExample("Swing Context Menu"); frame.setVisible(true); } } |
6.2 SwingPopupMenuExample.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package tube.coding.examples; import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JFrame; import javax.swing.JMenuItem; import javax.swing.JPopupMenu; @SuppressWarnings("serial") //Sample 02: Implement MouseListener public class SwingPopupMenuExample extends JFrame implements MouseListener { //Sample 03: Member Variable JPopupMenu popMnu; public SwingPopupMenuExample(String title) { //Sample 01: Set Size and Position super(title); setBounds(100, 100, 450, 250); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Sample 04: Create Popup Menu popMnu = new JPopupMenu(); JMenuItem miCut = new JMenuItem("Cut"); JMenuItem miCopy = new JMenuItem("Copy"); JMenuItem miPaste = new JMenuItem("Paste"); popMnu.add(miCut); popMnu.add(miCopy); popMnu.add(miPaste); //Sample 05: Add MouseListener addMouseListener(this); } @Override public void mouseClicked(MouseEvent e){} @Override public void mousePressed(MouseEvent e) { //Sample 06: Show Popup menu at right click location if (e.getButton() == MouseEvent.BUTTON3) popMnu.show(this, e.getX(), e.getY()); } @Override public void mouseReleased(MouseEvent e){} @Override public void mouseEntered(MouseEvent e) {} @Override public void mouseExited(MouseEvent e) {} } |
Categories: Swing
Tags: Context Menu, JPopupMenu, JPopupMenu::show, MouseListener, mousePressed