Java Utililty Methods JPopupMenu

List of utility methods to do JPopupMenu

Description

The list of methods to do JPopupMenu are organized into topic(s).

Method

voidshowPopupMenu(JPopupMenu popup, Component comp, int x, int y, boolean point)
Shows the specified popup menu, ensuring it is displayed within the bounds of the screen.
int offsetX = 0;
int offsetY = 0;
int extraOffset = point ? 1 : 0;
Component win = comp;
while (!(win instanceof Window || win == null)) {
    offsetX += win.getX();
    offsetY += win.getY();
    win = win.getParent();
...
voidshowPopupMenu(JPopupMenu popup, Component invoker, int x, int y)
Shows the popup menu with the consideration of the invoker's orientation.
popup.applyComponentOrientation(invoker.getComponentOrientation());
if (popup.getComponentOrientation().isLeftToRight()) {
    popup.show(invoker, x, y);
} else {
    popup.show(invoker, x - popup.getPreferredSize().width, y);
voidshowPopupPanel(Component activationComponent, JPopupMenu popup)
Show the panel popup component at a proper location.
if (popup == null || activationComponent == null)
    return;
Dimension dim = popup.getPreferredSize();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Point loc = activationComponent.getLocationOnScreen();
int x = 0;
if (loc.x + dim.width > screen.width) {
    x = activationComponent.getWidth() - dim.width; 
...
voidupdateOrInsertMenuItem(JPopupMenu menu, JMenuItem menuItem)
Depending on menuItem actionCommand, if a sub menu of menu has the same actionCommand then the already inserted menu item receive all the action listeners of the provided menuItem
updateOrInsertMenuItem(menu, menuItem, false);
booleanwillPopupBeContained(JPopupMenu popup, Point origin)
will Popup Be Contained
if (!popup.isShowing()) {
    return false;
Window w = SwingUtilities.windowForComponent(popup.getInvoker());
Rectangle r = new Rectangle(origin, popup.getSize());
return w != null && w.getBounds().contains(r);