Java JPopupMenu addPopup(Component component, final JPopupMenu popup)

Here you can find the source of addPopup(Component component, final JPopupMenu popup)

Description

Add the specified pop-up menu the specified component.

License

Open Source License

Parameter

Parameter Description
component the specified component
popup the specified pop-up menu

Declaration

public static void addPopup(Component component, final JPopupMenu popup) 

Method Source Code


//package com.java2s;
import java.awt.Component;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPopupMenu;

public class Main {
    /**//from   w  ww. ja v a  2 s. c  o  m
     * Add the specified pop-up menu the specified component.
     * 
     * @param component
     *            the specified component
     * @param popup
     *            the specified pop-up menu
     */
    public static void addPopup(Component component, final JPopupMenu popup) {
        component.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger())
                    showMenu(e);
            }

            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger())
                    showMenu(e);
            }

            private void showMenu(MouseEvent e) {
                popup.show(e.getComponent(), e.getX(), e.getY());
            }
        });
    }
}

Related

  1. addMenuItem(JPopupMenu menu, Action action)
  2. addMenuItems(JPopupMenu menu, JComponent... items)
  3. addOption(JPopupMenu popupMenu, String text, String key, ActionListener actionListener)
  4. addPopup(Component component, final JPopupMenu popup)
  5. addPopup(final Component c, final JPopupMenu m)
  6. addSeparatorIfNecessary(JPopupMenu popup)
  7. addSeparatorIfNecessary(JPopupMenu popup)