Example usage for java.awt Window setLocation

List of usage examples for java.awt Window setLocation

Introduction

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

Prototype

@Override
public void setLocation(int x, int y) 

Source Link

Document

The method changes the geometry-related data.

Usage

From source file:Main.java

/**
 * Centers a window on screen./* ww  w .  j a  v a2 s .c o m*/
 * <p>
 * @param w     The window to center.
 */
public static void centerOnScreen(Window w) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension splashSize = w.getPreferredSize();
    w.setLocation(screenSize.width / 2 - (splashSize.width / 2),
            screenSize.height / 2 - (splashSize.height / 2));
}

From source file:Main.java

/**
 * Center a window on screen./* www.  j  a  va  2  s . co m*/
 *
 * @param w
 *          the window to center on screen.
 */
public static void centerInParent(Window w) {
    Container parent = w.getParent();
    if (parent != null) {
        Dimension parentSize = parent.getSize();
        w.setLocation(parent.getX() + (parentSize.width - w.getWidth()) / 2,
                parent.getY() + (parentSize.height - w.getHeight()) / 2);
    }
}

From source file:Main.java

/**
 * Centers the given window on the primary screen.
 *
 * @param window/*w ww.  j a  va 2s  .c  om*/
 *            The window to center
 */
public static void center(Window window) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension windowSize = window.getSize();
    window.setLocation((screenSize.width - windowSize.width) / 2, (screenSize.height - windowSize.height) / 2);
}

From source file:Main.java

/**
 * This method packs, sets the size, and sets the position of the window given
 *
 * @param window//from  w  w w.j  a v  a  2s  .  c  o  m
 */
public static void centerAndPack(Window window) {
    window.pack();
    int x = getWindowXCenter() - (window.getWidth() / 2);
    int y = getWindowYCenter() - (window.getHeight() / 2);
    window.setLocation(x, y);
    window.setMinimumSize(new Dimension(window.getWidth(), window.getHeight()));
}

From source file:Main.java

/**
 * Center the given <code>windows</code> in relative to the given <code>invoker</code>.
 * @param invoker Reference window for the window to center.
 * @param window The window instance to center.
 *//*  w  ww  . ja  v  a  2s. c o m*/
public static void centerOnWindow(Window invoker, Window window) {
    Point invokerLocationOnScreen = invoker.getLocationOnScreen();
    Dimension invokerSize = invoker.getSize();
    Dimension windowSize = window.getSize();

    window.setLocation(invokerLocationOnScreen.x + (invokerSize.width / 2) - (windowSize.width / 2),
            invokerLocationOnScreen.y + ((invokerSize.height / 2) - (windowSize.height / 2) / 2));
}

From source file:Main.java

/**
 * Centraliza a janela na tela/*from w ww . j  a va 2  s  . com*/
 * @param w janela a ser centralizada
 */
public static void center(Window w) {
    Dimension us = w.getSize(), them = Toolkit.getDefaultToolkit().getScreenSize();
    int newX = (them.width - us.width) / 2;
    int newY = (them.height - us.height) / 2;
    w.setLocation(newX, newY);
}

From source file:Main.java

/**
 * Centers a given window on the screen.
 * //from   w w  w  .  j  a  v a2  s . c  o m
 * @param window
 *            The window, frame or dialog
 */
public static void centerOnScreen(Window window) {
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (int) ((d.getWidth() - window.getWidth()) / 2);
    int y = (int) ((d.getHeight() - window.getHeight()) / 2);
    window.setLocation(x, y);
}

From source file:Main.java

public static void centerContainer(Window window) {
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension size = window.getSize();

    int newWidth = (screen.width - size.width) / 2;
    int newHeight = (screen.height - size.height) / 2;
    window.setLocation(newWidth, newHeight);
}

From source file:Main.java

/**
 * Centers the given window over its owner window.
 *
 * @param window/*  w  w w . j  a  v a  2 s  .c o  m*/
 *            The window to center
 * @param ownerWindow
 *            The window to center the other window over
 */
public static void center(Window window, Window ownerWindow) {
    Point ownerWindowLocation = ownerWindow.getLocation();
    Dimension ownerWindowDimension = ownerWindow.getSize();
    Dimension windowSize = window.getSize();
    window.setLocation(ownerWindowLocation.x + (ownerWindowDimension.width - windowSize.width) / 2,
            ownerWindowLocation.y + (ownerWindowDimension.height - windowSize.height) / 2);
}

From source file:Main.java

/**
 * A version of setLocationRelativeTo() that takes the maximum window bounds into account
 * (taskbar)./*w  ww .  j  ava  2s.c  om*/
 */
public static void setLocationRelativeTo(Window window, Component component) {
    window.setLocationRelativeTo(component);
    Point screenLocation = getCurrentScreenLocation(window);
    if (window.getX() < screenLocation.x)
        window.setLocation(screenLocation.x, window.getY());
    if (window.getY() < screenLocation.y)
        window.setLocation(window.getX(), screenLocation.y);
}