Here you can find the source of showPopupMenu(JPopupMenu popup, Component comp, int x, int y, boolean point)
Parameter | Description |
---|---|
popup | The popup menu |
comp | The component to show it for |
x | The x co-ordinate |
y | The y co-ordinate |
point | If true, then the popup originates from a single point; otherwise it will originate from the component itself. This affects positioning in the case where the popup does not fit onscreen. |
public static void showPopupMenu(JPopupMenu popup, Component comp, int x, int y, boolean point)
//package com.java2s; /*// ww w . j a va2 s . c o m * GUIUtilities.java - Various GUI utility functions * :tabSize=4:indentSize=4:noTabs=false: * :folding=explicit:collapseFolds=1: * * Copyright (C) 1999, 2004 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.JPopupMenu; 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 * @see javax.swing.JComponent#setComponentPopupMenu(javax.swing.JPopupMenu) setComponentPopupMenu * which works better and is simpler to use: you don't have to write the code to * show/hide popups in response to mouse events anymore. */ public static void showPopupMenu(JPopupMenu popup, Component comp, int x, int y) { showPopupMenu(popup, comp, x, y, true); } /** * 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 * @param point If true, then the popup originates from a single point; * otherwise it will originate from the component itself. This affects * positioning in the case where the popup does not fit onscreen. * * @since jEdit 4.1pre1 */ public static void showPopupMenu(JPopupMenu popup, Component comp, int x, int y, boolean point) { int offsetX = 0; int offsetY = 0; int extraOffset = point ? 1 : 0; Component win = comp; while (!(win instanceof Window || win == null)) { offsetX += win.getX(); offsetY += win.getY(); win = win.getParent(); } if (win != null) { Dimension size = popup.getPreferredSize(); Rectangle screenSize = getScreenBounds(); if (x + offsetX + size.width + win.getX() > screenSize.width && x + offsetX + win.getX() >= size.width) { //System.err.println("x overflow"); if (point) x -= size.width + extraOffset; else x = win.getWidth() - size.width - offsetX + extraOffset; } else { x += extraOffset; } //System.err.println("y=" + y + ",offsetY=" + offsetY // + ",size.height=" + size.height // + ",win.height=" + win.getHeight()); if (y + offsetY + size.height + win.getY() > screenSize.height && y + offsetY + win.getY() >= size.height) { if (point) y = win.getHeight() - size.height - offsetY + extraOffset; else y = -size.height - 1; } else { y += extraOffset; } popup.show(comp, x, y); } else popup.show(comp, x + extraOffset, y + extraOffset); } /** * Returns the screen bounds, taking into account multi-screen * environments. * @since jEdit 4.3pre18 */ public static Rectangle getScreenBounds() { Rectangle bounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds(); GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices(); if (devices.length > 1) { for (GraphicsDevice device : devices) { for (GraphicsConfiguration config : device.getConfigurations()) bounds = bounds.union(config.getBounds()); } } return bounds; } }