Here you can find the source of showPopMenuWithParentWidth(JPopupMenu popup, Component parentComponent, int x, int y)
public static void showPopMenuWithParentWidth(JPopupMenu popup, Component parentComponent, int x, int y)
//package com.java2s; import javax.swing.*; import java.awt.*; public class Main { public static void showPopMenuWithParentWidth(JPopupMenu popup, Component parentComponent, int x, int y) { if (popup == null) {// check null. return; }/*from w w w . j a v a2 s . c o m*/ Dimension size = popup.getPreferredSize(); size.width = Math.max(size.width, parentComponent.getWidth()); popup.setPreferredSize(size); showPopupCloseMenu(popup, parentComponent); } public static void showPopupCloseMenu(JPopupMenu popup, Component parentComponent) { if (popup == null) {// check null. return; } Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Window frame = SwingUtilities.getWindowAncestor(parentComponent); int leftX = 0; int rightX = parentComponent.getLocation().x + frame.getLocation().x + popup.getPreferredSize().width; if (rightX > screenSize.width) { leftX = screenSize.width - rightX; } popup.show(parentComponent, leftX, parentComponent.getSize().height); } }