Java JPopupMenu setPopupMenu(final JComponent component, final JPopupMenu popup)

Here you can find the source of setPopupMenu(final JComponent component, final JPopupMenu popup)

Description

Gives a component a popup menu

License

Open Source License

Parameter

Parameter Description
component The target component
popup The popup menu

Declaration

public static void setPopupMenu(final JComponent component, final JPopupMenu popup) 

Method Source Code

//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());
                }
            }
        });
    }
}

Related

  1. removeConsecutiveSeparators(JPopupMenu popupMenu)
  2. removeExtraSeparators(JPopupMenu popup)
  3. removeTopAndBottomSeparators(JPopupMenu popupMenu)
  4. setLocationOnScreen(JPopupMenu visibleMenu, int x, int y)
  5. setPopupMenu(final JComponent component, final JPopupMenu popup)
  6. showButtonPopupMenu(AbstractButton button, JPopupMenu popup)
  7. showMenu(JPopupMenu menu, JButton button)
  8. showModal(javax.swing.JPopupMenu popupMenu, java.awt.Component invoker, java.awt.Point pt)
  9. showPopMenuWithParentWidth(JPopupMenu popup, Component parentComponent, int x, int y)