Here you can find the source of positionPopup(Component component, JPopupMenu jpm, int xCoord, int yCoord)
public static void positionPopup(Component component, JPopupMenu jpm, int xCoord, int yCoord)
//package com.java2s; /**//from w ww . j ava 2 s . com * Title: tn5250J * Copyright: Copyright (c) 2001,202,2003 * Company: * @author Kenneth J. Pouncey * @version 0.4 * * Description: * * 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, or (at your option) * 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 software; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * */ import java.awt.Component; import java.awt.Dimension; import java.awt.Point; import java.awt.Toolkit; import javax.swing.JPopupMenu; import javax.swing.SwingUtilities; public class Main { public static void positionPopup(Component component, JPopupMenu jpm, int xCoord, int yCoord) { Dimension popupSize = jpm.getSize(); if (popupSize.width == 0) popupSize = jpm.getPreferredSize(); Point point = new Point(xCoord + popupSize.width, yCoord + popupSize.height); SwingUtilities.convertPointToScreen(point, component); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int x = 0; int y = 0; if (point.y > screenSize.height - 25) y = yCoord - popupSize.height; if (point.x > screenSize.width) x = xCoord - popupSize.width; jpm.show(component, x != 0 ? x : xCoord, y != 0 ? y : yCoord); } }