Java JPopupMenu showPopMenuWithParentWidth(JPopupMenu popup, Component parentComponent, int x, int y)

Here you can find the source of showPopMenuWithParentWidth(JPopupMenu popup, Component parentComponent, int x, int y)

Description

show Pop Menu With Parent Width

License

Open Source License

Declaration

public static void showPopMenuWithParentWidth(JPopupMenu popup,
        Component parentComponent, int x, int y) 

Method Source Code

//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);
    }
}

Related

  1. setPopupMenu(final JComponent component, final JPopupMenu popup)
  2. setPopupMenu(final JComponent component, final JPopupMenu popup)
  3. showButtonPopupMenu(AbstractButton button, JPopupMenu popup)
  4. showMenu(JPopupMenu menu, JButton button)
  5. showModal(javax.swing.JPopupMenu popupMenu, java.awt.Component invoker, java.awt.Point pt)
  6. showPopup(JPopupMenu popup, MouseEvent event)
  7. showPopupCloseMenu(JPopupMenu popup, Component parentComponent)
  8. showPopupMenu(final JPopupMenu popup, final Component component, int x, int y)
  9. showPopupMenu(javax.swing.JPopupMenu popup, Component comp, int x, int y)