Java tutorial
//package com.java2s; import java.awt.Component; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JPopupMenu; public class Main { /** * Adds a MouseListener to the component specified that will show the popup * specified (at the position that the mouse was clicked) when the mouse is * right-clicked, or whatever mouse event returns true from the * {@link MouseEvent#isPopupTrigger()} method.<br/><br/> * * @param c * The component to add the mouse listener to * @param popup * the popup to show whe the component is clicked */ public static void addPopup(Component c, final JPopupMenu popup) { c.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { if (e.isPopupTrigger()) popup.show(e.getComponent(), e.getX(), e.getY()); } @Override public void mouseReleased(MouseEvent e) { mousePressed(e); } @Override public void mouseClicked(MouseEvent e) { mousePressed(e); } }); } }