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 w w w . j a va 2s. c o m * Copyright (C) 2010-2012 Klaus Reimer <k@ailis.de> * See LICENSE.TXT for licensing information. */ 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) { // Ignore mouse buttons outside of the normal range. This // fixes problems with trackpad scrolling. if (e.getButton() > MouseEvent.BUTTON3) return; if (e.isPopupTrigger()) { popup.show(component, e.getX(), e.getY()); } } @Override public void mouseReleased(final MouseEvent e) { // Ignore mouse buttons outside of the normal range. This // fixes problems with trackpad scrolling. if (e.getButton() > MouseEvent.BUTTON3) return; if (e.isPopupTrigger()) { popup.show(component, e.getX(), e.getY()); } } }); } }