Example usage for java.awt Window setBounds

List of usage examples for java.awt Window setBounds

Introduction

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

Prototype

public void setBounds(Rectangle r) 

Source Link

Document

The r.width or r.height values will be automatically enlarged if either is less than the minimum size as specified by previous call to setMinimumSize .

Usage

From source file:Main.java

public static void centerFrame(final Window frame) {
    final Rectangle bounds = frame.getBounds();
    final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    bounds.x = (screen.width / 2) - (bounds.width / 2);
    bounds.y = (screen.height / 2) - (bounds.height / 2);
    frame.setBounds(bounds);
}

From source file:Main.java

public static void centerAndSizeWindow(Window win, int fraction, int base) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int width = screenSize.width * fraction / base;
    int height = screenSize.height * fraction / base;

    Rectangle rect = new Rectangle((screenSize.width - width) / 2, (screenSize.height - height) / 2, width,
            height);// ww w.j  a v a 2s  . c o m
    win.setBounds(rect);
}

From source file:AWTUtilities.java

/**
 * Reposition the specified window so that it necessarily fits on the screen,
 * while trying to minimize changes.//from   w w w.  j  a v  a2  s  .c  o m
 */

public static void forceToScreen(Window window) {
    Dimension screenSize = window.getToolkit().getScreenSize();
    Rectangle bounds = window.getBounds();

    bounds.width = Math.min(bounds.width, screenSize.width);
    bounds.height = Math.min(bounds.height, screenSize.height);
    bounds.x = Math.min(Math.max(bounds.x, 0), screenSize.width - bounds.width);
    bounds.y = Math.min(Math.max(bounds.y, 0), screenSize.height - bounds.height);

    window.setBounds(bounds);
}

From source file:org.pentaho.reporting.libraries.designtime.swing.LibSwingUtil.java

public static boolean safeRestoreWindow(final Window frame, final Rectangle bounds) {
    final GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    final GraphicsDevice[] devices = graphicsEnvironment.getScreenDevices();
    for (int i = 0; i < devices.length; i++) {
        final GraphicsDevice device = devices[i];
        final Rectangle rectangle = device.getDefaultConfiguration().getBounds();
        if (rectangle.contains(bounds) || rectangle.equals(bounds)) {
            logger.info("Found a usable screen-configuration: Restoring frame to " + bounds);
            frame.setBounds(bounds);
            return true;
        }/*from  ww  w  . j  av a2s  .  c o  m*/
    }
    return false;
}