Example usage for java.awt Container setLocation

List of usage examples for java.awt Container setLocation

Introduction

In this page you can find the example usage for java.awt Container setLocation.

Prototype

public void setLocation(int x, int y) 

Source Link

Document

Moves this component to a new location.

Usage

From source file:Main.java

public static void showOnSameScreenAsMouseCenter(Container frame) {
    Point mouseLocation = MouseInfo.getPointerInfo().getLocation();

    GraphicsDevice device;/*  w w w  .ja va  2 s .  c  o m*/

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice lstGDs[] = ge.getScreenDevices();

    ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);

    for (GraphicsDevice gd : lstGDs) {
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        Rectangle screenBounds = gc.getBounds();

        if (screenBounds.contains(mouseLocation)) {
            lstDevices.add(gd);
        }
    }

    if (lstDevices.size() > 0) {
        device = lstDevices.get(0);
    } else {
        device = ge.getDefaultScreenDevice();
    }

    Rectangle bounds = device.getDefaultConfiguration().getBounds();
    frame.setLocation(bounds.x + bounds.width / 2 - frame.getWidth() / 2,
            bounds.y + bounds.height / 2 - frame.getHeight() / 2);
}