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 taken from jEdit 4.0pre1

Declaration

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

Method Source Code


//package com.java2s;
/*//from w w w.ja v a 2s.  c  om
 * GraphicUtils.java - SEdit, a tool to design and animate graphs in MadKit
 * Copyright (C) 1998-2002 Jacques Ferber, Olivier Gutknecht
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library 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 Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.awt.*;

import javax.swing.*;

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 taken from 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. Taken from jEdit 4.0
     */
    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) {
                if (point)
                    x -= (size.width + extraOffset);
                else
                    x = (win.getWidth() - size.width - offsetX + extraOffset);
            } else {
                x += extraOffset;
            }

            if (y + offsetY + size.height > win.getHeight() && y + offsetY >= size.height) {
                if (point)
                    y = (win.getHeight() - size.height - offsetY + extraOffset);
            } else {
                y += extraOffset;
            }

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

    }
}

Related

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