Here you can find the source of setPopupMenu(final JComponent component, final JPopupMenu popup)
Parameter | Description |
---|---|
component | The target component |
popup | The popup menu |
public static void setPopupMenu(final JComponent component, final JPopupMenu popup)
//package com.java2s; /*//from www .j a v a 2s .c o m * Copyright (C) 2009 Klaus Reimer <k@ailis.de> * See LICENSE.md for licensing information. */ import java.awt.TrayIcon; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JComponent; import javax.swing.JPopupMenu; public class Main { /** * Gives a component a popup menu * * @param component * The target component * @param popup * The popup menu */ public static void setPopupMenu(final JComponent component, final JPopupMenu popup) { component.addMouseListener(new MouseAdapter() { @Override public void mousePressed(final MouseEvent e) { if (e.isPopupTrigger()) { popup.show(component, e.getX(), e.getY()); } } @Override public void mouseReleased(final MouseEvent e) { if (e.isPopupTrigger()) { popup.show(component, e.getX(), e.getY()); } } }); } /** * Gives a tray icon a popup menu. * * @param trayIcon * The tray icon * @param popup * The popup menu */ public static void setPopupMenu(final TrayIcon trayIcon, final JPopupMenu popup) { trayIcon.addMouseListener(new MouseAdapter() { @Override public void mousePressed(final MouseEvent e) { if (e.isPopupTrigger()) { popup.setLocation(e.getX(), e.getY()); popup.setInvoker(popup); popup.setVisible(true); } } @Override public void mouseReleased(final MouseEvent e) { mousePressed(e); } }); } }