Here you can find the source of showPopupMenu(final JPopupMenu popup, final Component component, int x, int y)
public static void showPopupMenu(final JPopupMenu popup, final Component component, int x, int y)
//package com.java2s; 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 showPopupMenu(final JPopupMenu popup, final Component component, int x, int y) { final Point p = new Point(x, y); SwingUtilities.convertPointToScreen(p, component); final Dimension size = popup.getPreferredSize(); final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); boolean horiz = false; boolean vert = false; final int origX = x; if ((p.x + size.width > screen.width) && (size.width < screen.width)) { x += (screen.width - p.x - size.width); horiz = true;//ww w . j a v a2 s .c o m } if ((p.y + size.height > screen.height) && (size.height < screen.height)) { y += (screen.height - p.y - size.height); vert = true; } if (horiz && vert) { x = origX - size.width - 2; } popup.show(component, x, y); } }