static class PopupMenuHelper extends MouseAdapter { JPopupMenu menu; Point point; /** override me */ swappable void fillMenu() { } public void mousePressed(MouseEvent e) { displayMenu(e); } public void mouseReleased(MouseEvent e) { displayMenu(e); } private void displayMenu(MouseEvent e) { boolean popupTrigger = e.isPopupTrigger(); if (popupTrigger) { JPopupMenu menu = new JPopupMenu(); int count = menu.getComponentCount(); this.menu = menu; this.point = e.getPoint(); fillMenu(); this.menu = null; if (menu.getComponentCount() != count) menu.show(e.getComponent(), e.getX(), e.getY()); } } public void install(JComponent component) { component.addMouseListener(this); } }