Java JPopupMenu showPopupMenu(JPopupMenu popup, Component comp, int x, int y)

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

Description

Shows the specified popup menu, ensuring it is displayed within the bounds of the screen.

License

Open Source License

Parameter

Parameter Description
popup The popup menu
comp The component to show it for
x The x co-ordinate
y The y co-ordinate

Declaration

public static void showPopupMenu(JPopupMenu popup, Component comp, int x, int y) 

Method Source Code


//package com.java2s;
/*//  www  . j av a  2s  . com
 * GUIUtilities.java - Various GUI utility functions
 * :tabSize=8:indentSize=8:noTabs=false:
 * :folding=explicit:collapseFolds=1:
 *
 * Copyright (C) 1999, 2004 Slava Pestov
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.awt.Component;
import java.awt.Dimension;

import java.awt.Rectangle;
import java.awt.Window;

import javax.swing.JPopupMenu;

public class Main {
    /**
     * Shows the specified popup menu, ensuring it is displayed within
     * the bounds of the screen.
     * @param popup The popup menu
     * @param comp The component to show it for
     * @param x The x co-ordinate
     * @param y The y co-ordinate
     * @since jEdit 4.0pre1
     */
    public static void showPopupMenu(JPopupMenu popup, Component comp, int x, int y) {
        showPopupMenu(popup, comp, x, y, true);
    }

    /**
     * Shows the specified popup menu, ensuring it is displayed within
     * the bounds of the screen.
     * @param popup The popup menu
     * @param comp The component to show it for
     * @param x The x co-ordinate
     * @param y The y co-ordinate
     * @param point If true, then the popup originates from a single point;
     * otherwise it will originate from the component itself. This affects
     * positioning in the case where the popup does not fit onscreen.
     *
     * @since jEdit 4.1pre1
     */
    public static void showPopupMenu(JPopupMenu popup, Component comp, int x, int y, boolean point) {
        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();
        }

        if (win != null) {
            Dimension size = popup.getPreferredSize();

            Rectangle screenSize = win.getGraphicsConfiguration().getBounds();

            if (x + offsetX + size.width + win.getX() > screenSize.width
                    && x + offsetX + win.getX() >= size.width) {
                //System.err.println("x overflow");
                if (point)
                    x -= (size.width + extraOffset);
                else
                    x = (win.getWidth() - size.width - offsetX + extraOffset);
            } else {
                x += extraOffset;
            }

            //System.err.println("y=" + y + ",offsetY=" + offsetY
            //   + ",size.height=" + size.height
            //   + ",win.height=" + win.getHeight());
            if (y + offsetY + size.height + win.getY() > screenSize.height
                    && y + offsetY + win.getY() >= size.height) {
                if (point)
                    y = (win.getHeight() - size.height - offsetY + extraOffset);
                else
                    y = -size.height - 1;
            } else {
                y += extraOffset;
            }

            popup.show(comp, x, y);
        } else
            popup.show(comp, x + extraOffset, y + extraOffset);

    }
}

Related

  1. showPopupMenu(final JPopupMenu popup, final Component component, int x, int y)
  2. showPopupMenu(javax.swing.JPopupMenu popup, Component comp, int x, int y)
  3. showPopupMenu(javax.swing.JPopupMenu popup, Component comp, int x, int y)
  4. showPopupMenu(JPopupMenu menu, Component invoker, MouseEvent e)
  5. showPopupMenu(JPopupMenu popup, Component comp, int x, int y)
  6. showPopupMenu(JPopupMenu popup, Component comp, int x, int y)
  7. showPopupMenu(JPopupMenu popup, Component comp, int x, int y, boolean point)
  8. showPopupMenu(JPopupMenu popup, Component invoker, int x, int y)
  9. showPopupPanel(Component activationComponent, JPopupMenu popup)