Here you can find the source of ensurePopupIsOnScreen(JPopupMenu popup)
public static void ensurePopupIsOnScreen(JPopupMenu popup)
//package com.java2s; import javax.swing.JPopupMenu; public class Main { public static void ensurePopupIsOnScreen(JPopupMenu popup) { java.awt.Point location = popup.getLocation(null); java.awt.Dimension size = popup.getSize(null); java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); screenSize.height -= 28; // hack for standard Windows Task Bar javax.swing.SwingUtilities.convertPointToScreen(location, popup); if (location.x < 0) { location.x = 0;//from w ww . j a va2 s .com } else if (location.x + size.width > screenSize.width) { location.x -= (location.x + size.width) - screenSize.width; } if (location.y < 0) { location.y = 0; } else if (location.y + size.height > screenSize.height) { location.y -= (location.y + size.height) - screenSize.height; } popup.setLocation(location); } }