Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.awt.Component;
import java.awt.Dimension;

import java.awt.Point;

import java.awt.Toolkit;

import javax.swing.JPopupMenu;

import javax.swing.SwingUtilities;

public class Main {
    public static void showPopupMenu(final JPopupMenu popup, final Component component, int x, int y) {
        final Point p = new Point(x, y);
        SwingUtilities.convertPointToScreen(p, component);
        final Dimension size = popup.getPreferredSize();
        final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        boolean horiz = false;
        boolean vert = false;

        final int origX = x;

        if ((p.x + size.width > screen.width) && (size.width < screen.width)) {
            x += (screen.width - p.x - size.width);
            horiz = true;
        }

        if ((p.y + size.height > screen.height) && (size.height < screen.height)) {
            y += (screen.height - p.y - size.height);
            vert = true;
        }

        if (horiz && vert) {
            x = origX - size.width - 2;
        }
        popup.show(component, x, y);
    }
}