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

public static void centerInParent(Window window, Component myParent) {
    int x;/*from w ww  .  j av  a2  s .co m*/
    int y;

    Point topLeft = myParent.getLocationOnScreen();
    Dimension parentSize = myParent.getSize();
    Dimension mySize = window.getSize();
    if (parentSize.width > mySize.width)
        x = ((parentSize.width - mySize.width) / 2) + topLeft.x;
    else
        x = topLeft.x;

    if (parentSize.height > mySize.height)
        y = ((parentSize.height - mySize.height) / 2) + topLeft.y;
    else
        y = topLeft.y;

    window.setLocation(x, y);
}

From source file:Main.java

public static void center(Window frame, int left, int right, int top, int bottom) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    if (frameSize.height > (screenSize.height - top - bottom)) {
        frameSize.height = screenSize.height - top - bottom;
    }/*from   w  ww  . j  ava 2s  .  c o m*/
    if (frameSize.width > screenSize.width - left - right) {
        frameSize.width = screenSize.width - left - right;
    }
    if (!frameSize.equals(frame.getSize())) {
        frame.setSize(frameSize);
    }
    frame.setLocation(left + (screenSize.width - frameSize.width) / 2,
            top + (screenSize.height - frameSize.height) / 2);
}

From source file:AWTUtilities.java

/**
 * Packs and centers the given window relative to the given component. The
 * specified component may be <code>null</code>, in which case the window will
 * be centered on the screen. The method also makes sure that the target
 * window is fully visible by calling <code>forceToScreen</code>.
 *///  w w w. j  a v  a  2s.  c  o  m

public static void centerWindow(Window target, Component parent) {
    target.pack();

    Dimension size = target.getSize();
    Rectangle parentBounds = parent == null || !parent.isShowing() ? getUsableScreenBounds()
            : new Rectangle(parent.getLocationOnScreen(), parent.getSize());

    target.setLocation(parentBounds.x + (parentBounds.width - size.width) / 2,
            parentBounds.y + (parentBounds.height - size.height) / 2);

    forceToScreen(target);
}

From source file:Main.java

/**
 *  Centers a window within a specified space. If the window is larger than
 *  the width/height of the space, it may optionally overflow: its X and Y
 *  will be less than those passed. However, it is not allowed to overflow
 *  to negative coordinates./*w  ww .j av a 2s  .c  o  m*/
 */
private static void center(Window window, Rectangle bounds, boolean allowOverflow) {
    Dimension windowSize = window.getSize();

    int offsetX = (bounds.width - windowSize.width) / 2;
    if ((offsetX < 0) && !allowOverflow) {
        offsetX = 0;
    }

    int x = bounds.x + offsetX;
    if (x < 0) {
        x = 0;
    }

    int offsetY = (bounds.height - windowSize.height) / 2;
    if ((offsetY < 0) && !allowOverflow) {
        offsetY = 0;
    }

    int y = bounds.y + offsetY;
    if (y < 0) {
        y = 0;
    }

    window.setLocation(x, y);
}

From source file:SwingUtils.java

public static void centerOnScreen(Window window, boolean packFrame) {
    //Validate frames that have preset sizes
    //Pack frames that have useful preferred size info, e.g. from their layout
    if (packFrame) {
        window.pack();/*from w w w  . j  a  v  a2  s  . co m*/
    } else {
        window.validate();
    }

    //Center the frame window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = window.getSize();

    if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
    }

    if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
    }

    window.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
}

From source file:Main.java

/**
 * Center window on screen./*from   w  w  w  .ja v a 2s.c  o  m*/
 *
 * @param window    window to be centered.
 * @param packFrame if <code>true</code> call window's <code>pack()</code>
 *                  method before centering.
 */
public static void centerOnScreen(Window window, boolean packFrame) {
    //Validate frames that have preset sizes
    //Pack frames that have useful preferred size info, e.g. from their layout
    if (packFrame) {
        window.pack();
    } else {
        window.validate();
    }

    //Center the frame window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = window.getSize();

    if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
    }

    if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
    }

    window.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
}

From source file:Win.java

public static void position(Window frame, Component ref, double xfrac, double yfrac) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension size = frame.getSize();
    Dimension refSize = (ref != null) ? ref.getSize() : screenSize;
    Point origin = (ref != null) ? ref.getLocationOnScreen() : new Point(0, 0);

    int x = origin.x + relativePoint(xfrac, refSize.width, size.width);
    int y = origin.y + relativePoint(yfrac, refSize.height, size.height);

    // make sure frame is entirely on screen
    x = Math.max(0, Math.min(screenSize.width - size.width, x));
    y = Math.max(0, Math.min(screenSize.height - size.height, y));

    frame.setLocation(x, y);
}

From source file:it.geosolutions.imageio.plugins.nitronitf.ImageIOUtils.java

public static void centerWindow(Window w) {
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension window = w.getSize();
    if (window.width == 0)
        return;//  ww  w  . ja v  a2 s .c om
    int left = screen.width / 2 - window.width / 2;
    int top = (screen.height - window.height) / 4;
    if (top < 0)
        top = 0;
    w.setLocation(left, top);
}

From source file:Main.java

public static void centerWindow(Window window, int width, int height) {
    // Get screen size
    final Toolkit tk = Toolkit.getDefaultToolkit();
    final Dimension screensize = tk.getScreenSize();

    // Set window minimum size
    window.setMinimumSize(new Dimension((int) Math.floor(screensize.getWidth() * 0.3),
            (int) Math.floor(screensize.getHeight() * 0.3)));

    // Set window size
    if (width == 0f)
        width = (int) Math.floor(screensize.getWidth() * 0.8);

    if (height == 0f)
        height = (int) Math.floor(screensize.getHeight() * 0.8);

    window.setPreferredSize(new Dimension(width, height));

    int left = (int) (screensize.getWidth() - width) / 2;
    int right = (int) (screensize.getHeight() - height) / 2;
    ;//from   ww  w  . j a  v  a  2  s .  c o m

    // Center window
    window.setLocation(left, right);
}

From source file:Main.java

public static void setWindowLocationRelativeTo(Window window, Window relativeWindow) {
    Rectangle windowBounds = window.getBounds();
    Dimension rwSize = relativeWindow.getSize();
    Point rwLoc = relativeWindow.getLocation();

    int dx = rwLoc.x + ((rwSize.width - windowBounds.width) >> 1);
    int dy = rwLoc.y + ((rwSize.height - windowBounds.height) >> 1);
    Dimension ss = window.getToolkit().getScreenSize();

    if (dy + windowBounds.height > ss.height) {
        dy = ss.height - windowBounds.height;
        dx = rwLoc.x < (ss.width >> 1) ? rwLoc.x + rwSize.width : rwLoc.x - windowBounds.width;
    }//from  w  w  w .  ja  v  a  2  s .com
    if (dx + windowBounds.width > ss.width) {
        dx = ss.width - windowBounds.width;
    }
    if (dx < 0) {
        dx = 0;
    }
    if (dy < 0) {
        dy = 0;
    }
    window.setLocation(dx, dy);
}