Here you can find the source of getPopupMenuShowPoint(JPopupMenu popup, int x, int y)
Parameter | Description |
---|---|
popup | the popup menu |
x | the x position in screen coordinate |
y | the y position in screen coordinates |
public static Point getPopupMenuShowPoint(JPopupMenu popup, int x, int y)
//package com.java2s; /**//from w w w.j a va2 s. c o m * $RCSfile: ,v $ * $Revision: $ * $Date: $ * * Copyright (C) 2004-2011 Jive Software. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.awt.Component; import java.awt.Dimension; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Point; import java.awt.Rectangle; import java.awt.event.MouseEvent; import javax.swing.JPopupMenu; public class Main { /** * Returns a point where the given popup menu should be shown. The point is * calculated by adjusting the X and Y coordinates from the given mouse * event so that the popup menu will not be clipped by the screen * boundaries. * * @param popup * the popup menu * @param event * the mouse event * @return the point where the popup menu should be shown */ public static Point getPopupMenuShowPoint(JPopupMenu popup, MouseEvent event) { Component source = (Component) event.getSource(); Point topLeftSource = source.getLocationOnScreen(); Point ptRet = getPopupMenuShowPoint(popup, topLeftSource.x + event.getX(), topLeftSource.y + event.getY()); ptRet.translate(-topLeftSource.x, -topLeftSource.y); return ptRet; } /** * Returns a point where the given popup menu should be shown. The point is * calculated by adjusting the X and Y coordinates so that the popup menu * will not be clipped by the screen boundaries. * * @param popup * the popup menu * @param x * the x position in screen coordinate * @param y * the y position in screen coordinates * @return the point where the popup menu should be shown in screen * coordinates */ public static Point getPopupMenuShowPoint(JPopupMenu popup, int x, int y) { Dimension sizeMenu = popup.getPreferredSize(); Point bottomRightMenu = new Point(x + sizeMenu.width, y + sizeMenu.height); Rectangle[] screensBounds = getScreenBounds(); int n = screensBounds.length; for (int i = 0; i < n; i++) { Rectangle screenBounds = screensBounds[i]; if (screenBounds.x <= x && x <= (screenBounds.x + screenBounds.width)) { Dimension sizeScreen = screenBounds.getSize(); sizeScreen.height -= 32; // Hack to help prevent menu being // clipped by Windows/Linux/Solaris // Taskbar. int xOffset = 0; if (bottomRightMenu.x > (screenBounds.x + sizeScreen.width)) xOffset = -sizeMenu.width; int yOffset = 0; if (bottomRightMenu.y > (screenBounds.y + sizeScreen.height)) yOffset = sizeScreen.height - bottomRightMenu.y; return new Point(x + xOffset, y + yOffset); } } return new Point(x, y); // ? that would mean that the top left point was // not on any screen. } /** * Returns the ScreenBounds * * @return Array of {@link Rectangle}'s */ public static Rectangle[] getScreenBounds() { GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment(); final GraphicsDevice[] screenDevices = graphicsEnvironment.getScreenDevices(); Rectangle[] screenBounds = new Rectangle[screenDevices.length]; for (int i = 0; i < screenDevices.length; i++) { GraphicsDevice screenDevice = screenDevices[i]; final GraphicsConfiguration defaultConfiguration = screenDevice.getDefaultConfiguration(); screenBounds[i] = defaultConfiguration.getBounds(); } return screenBounds; } }