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

Related

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