Java JPopupMenu getMousePopupAdapter(final Component component, final JPopupMenu popup)

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

Description

Creates a mouse popup adapter for the given component and popup menu.

License

Open Source License

Parameter

Parameter Description
component the target component, non null
popup the popup menu, non null

Return

the mouse adapter

Declaration

public static MouseAdapter getMousePopupAdapter(final Component component, final JPopupMenu popup) 

Method Source Code


//package com.java2s;
/*/*  ww  w.ja  va2s. co  m*/
 * Copyright 2010-2012 The Advance EU 7th Framework project consortium
 *
 * This file is part of Advance.
 *
 * Advance is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * Advance is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Advance.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */

import java.awt.Component;

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

import javax.swing.JPopupMenu;

public class Main {
    /**
     * Creates a mouse popup adapter for the given component and popup menu.
     * @param component the target component, non null
     * @param popup the popup menu, non null
     * @return the mouse adapter
     */
    public static MouseAdapter getMousePopupAdapter(final Component component, final JPopupMenu popup) {
        return new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                handlePopup(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                handlePopup(e);
            }

            private void handlePopup(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popup.show(component, e.getX(), e.getY());
                }
            }
        };
    }
}

Related

  1. createPopupMenu()
  2. createPopupMenu(String menuText, JPopupMenu menu, ActionListener listener)
  3. ensurePopupIsOnScreen(JPopupMenu popup)
  4. findMenuComponent(JPopupMenu menu, String menuComponentName)
  5. findSubMenu(JPopupMenu popupMenu, String name)
  6. getOnlyPopupMenu(Container owner)
  7. getPopupMenu()
  8. getPopupMenuShowPoint(JPopupMenu popup, int x, int y)
  9. insertSeparatorIfNeeded(JPopupMenu popupMenu, int position)