Here you can find the source of showPopupMenu(JPopupMenu popup, Component comp, int x, int y)
Parameter | Description |
---|---|
popup | The popup menu |
comp | The component to show it for |
x | The x co-ordinate |
y | The y co-ordinate |
public static void showPopupMenu(JPopupMenu popup, Component comp, int x, int y)
//package com.java2s; /*// ww w. j a va 2s .c om * GUIUtilities.java - Various GUI utility functions * :tabSize=8:indentSize=8:noTabs=false: * :folding=explicit:collapseFolds=1: * * Copyright (C) 1999, 2000, 2001, 2002 Slava Pestov * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ import javax.swing.*; import java.awt.*; public class Main { /** * Shows the specified popup menu, ensuring it is displayed within * the bounds of the screen. * @param popup The popup menu * @param comp The component to show it for * @param x The x co-ordinate * @param y The y co-ordinate * @since jEdit 4.0pre1 */ public static void showPopupMenu(JPopupMenu popup, Component comp, int x, int y) { Point p = new Point(x, y); SwingUtilities.convertPointToScreen(p, comp); Dimension size = popup.getPreferredSize(); Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); boolean horiz = false; boolean vert = false; // might need later int origX = x; if (p.x + size.width > screen.width && size.width < screen.width) { x += (screen.width - p.x - size.width); horiz = true; } if (p.y + size.height > screen.height && size.height < screen.height) { y += (screen.height - p.y - size.height); vert = true; } // If popup needed to be moved both horizontally and // vertically, the mouse pointer might end up over a // menu item, which will be invoked when the mouse is // released. This is bad, so move popup to a different // location. if (horiz && vert) { x = origX - size.width - 2; } popup.show(comp, x, y); } }