Here you can find the source of showPopupMenu(javax.swing.JPopupMenu popup, Component comp, int x, int y)
Parameter | Description |
---|---|
popup | The popup menu |
comp | The component to show it for |
x | The x coordinate |
y | The y coordinate |
public static void showPopupMenu(javax.swing.JPopupMenu popup, Component comp, int x, int y)
//package com.java2s; import java.awt.*; public class Main { /**// ww w .j a v a 2 s. c o m * 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 coordinate * @param y The y coordinate */ public static void showPopupMenu(javax.swing.JPopupMenu popup, Component comp, int x, int y) { Point p = getBestAnchorPoint(comp, x, y); popup.show(comp, p.x, p.y); popup.show(comp, x, y); } /** * Calculates the best location to show the component based on the given (x, y) * coordinates. The returned point will be as close as possible to the original * point while allowing the entire component to be displayed on screen. This is * useful for showing dialogs and popups. * @param comp the component that will be shown. * @param x the original x-coordinate of the component or of the desired location. * @param y the original y-coordinate of the component or of the desired location. * @return a point as close to the given (x, y) that will allow the entire * component to be shown on the screen. */ public static Point getBestAnchorPoint(Component comp, int x, int y) { int new_x = x; int new_y = y; Point p = new Point(new_x, new_y); javax.swing.SwingUtilities.convertPointToScreen(p, comp); Dimension size = comp.getSize(); Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); boolean move_horizontally = false; boolean move_vertically = false; // calculate new x coordinate. If the component width is less than the // screen width and the right side of the component is off the screen, // move it left. if (p.x + size.width > screen.width && size.width < screen.width) { new_x += (screen.width - p.x - size.width); move_horizontally = true; } // calculate new y coordinate. If the component height is less than the // screen height and the bottom of the component is off the screen, move // it up. if (p.y + size.height > screen.height && size.height < screen.height) { new_y += (screen.height - p.y - size.height); move_vertically = true; } // If the component is a popup and it 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. In this case, move the // component to a location that is not under the point. if (move_horizontally && move_vertically && (comp instanceof javax.swing.JPopupMenu)) { // first try to move it more left if (x - size.width - 2 > 0) new_x = x - size.width - 2; else if (y - size.height - 2 > 0) { // try to move it up some more new_y = y - size.height - 2; } } return new Point(new_x, new_y); } }