Java Code Examples for java.awt.Window#setLocationRelativeTo()

The following examples show how to use java.awt.Window#setLocationRelativeTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BackgroundIsNotUpdated.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(final String[] args) throws AWTException {
    final Window window = new BackgroundIsNotUpdated(null);
    window.setSize(300, 300);
    window.setLocationRelativeTo(null);
    window.setVisible(true);
    sleep();
    window.setBackground(Color.GREEN);
    sleep();
    final Robot robot = new Robot();
    robot.setAutoDelay(200);
    Point point = window.getLocationOnScreen();
    Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
                                      point.y + window.getHeight() / 2);
    window.dispose();
    if (!color.equals(Color.GREEN)) {
        throw new RuntimeException(
                "Expected: " + Color.GREEN + " , Actual: " + color);
    }
}
 
Example 2
Source File: BackgroundIsNotUpdated.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(final String[] args) throws AWTException {
    final Window window = new BackgroundIsNotUpdated(null);
    window.setSize(300, 300);
    window.setLocationRelativeTo(null);
    window.setVisible(true);
    sleep();
    window.setBackground(Color.GREEN);
    sleep();
    final Robot robot = new Robot();
    robot.setAutoDelay(200);
    Point point = window.getLocationOnScreen();
    Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
                                      point.y + window.getHeight() / 2);
    window.dispose();
    if (!color.equals(Color.GREEN)) {
        throw new RuntimeException(
                "Expected: " + Color.GREEN + " , Actual: " + color);
    }
}
 
Example 3
Source File: LGuiUtils.java    From scelight with Apache License 2.0 6 votes vote down vote up
/**
 * Resizes a window by setting its bounds to maximum that fits inside the default screen having the specified margin around it, and centers the window on
 * the screen.
 * 
 * <p>
 * The implementation takes the screen insets (for example space occupied by task bar) into account.
 * </p>
 * 
 * @param window window to be resized
 * @param margin margin to leave around the window
 * @param maxSize optional parameter defining a maximum size
 */
public static void maximizeWindowWithMargin( final Window window, final int margin, final Dimension maxSize ) {
	// Maybe use window.getGraphicsConfiguration() (it probably accounts multi-screens!)
	// Edit: it does, but setLocationRelativeTo() at the end will always use the default screen device!
	GraphicsConfiguration gconfig = window.getGraphicsConfiguration();
	if ( gconfig == null )
		GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
		
	final Rectangle bounds = gconfig.getBounds();
	final Insets insets = Toolkit.getDefaultToolkit().getScreenInsets( gconfig );
	
	final int width = bounds.width - insets.left - insets.right - margin * 2;
	final int height = bounds.height - insets.top - insets.bottom - margin * 2;
	
	if ( maxSize == null )
		window.setSize( width, height );
	else
		window.setSize( Math.min( width, maxSize.width ), Math.min( height, maxSize.height ) );
		
	// And finally center on screen
	// window.setLocationRelativeTo( null ) always centers on the main screen, so:
	window.setLocationRelativeTo( window.getOwner() );
}
 
Example 4
Source File: BackgroundIsNotUpdated.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(final String[] args) throws AWTException {
    final Window window = new BackgroundIsNotUpdated(null);
    window.setSize(300, 300);
    window.setLocationRelativeTo(null);
    window.setVisible(true);
    sleep();
    window.setBackground(Color.GREEN);
    sleep();
    final Robot robot = new Robot();
    robot.setAutoDelay(200);
    Point point = window.getLocationOnScreen();
    Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
                                      point.y + window.getHeight() / 2);
    window.dispose();
    if (!color.equals(Color.GREEN)) {
        throw new RuntimeException(
                "Expected: " + Color.GREEN + " , Actual: " + color);
    }
}
 
Example 5
Source File: BackgroundIsNotUpdated.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(final String[] args) throws AWTException {
    final Window window = new BackgroundIsNotUpdated(null);
    window.setSize(300, 300);
    window.setLocationRelativeTo(null);
    window.setVisible(true);
    sleep();
    window.setBackground(Color.GREEN);
    sleep();
    final Robot robot = new Robot();
    robot.setAutoDelay(200);
    Point point = window.getLocationOnScreen();
    Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
                                      point.y + window.getHeight() / 2);
    window.dispose();
    if (!color.equals(Color.GREEN)) {
        throw new RuntimeException(
                "Expected: " + Color.GREEN + " , Actual: " + color);
    }
}
 
Example 6
Source File: BackgroundIsNotUpdated.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public static void main(final String[] args) throws AWTException {
    final Window window = new BackgroundIsNotUpdated(null);
    window.setSize(300, 300);
    window.setLocationRelativeTo(null);
    window.setVisible(true);
    sleep();
    window.setBackground(Color.GREEN);
    sleep();
    final Robot robot = new Robot();
    robot.setAutoDelay(200);
    Point point = window.getLocationOnScreen();
    Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
                                      point.y + window.getHeight() / 2);
    window.dispose();
    if (!color.equals(Color.GREEN)) {
        throw new RuntimeException(
                "Expected: " + Color.GREEN + " , Actual: " + color);
    }
}
 
Example 7
Source File: BackgroundIsNotUpdated.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(final String[] args) throws AWTException {
    final Window window = new BackgroundIsNotUpdated(null);
    window.setSize(300, 300);
    window.setLocationRelativeTo(null);
    window.setVisible(true);
    sleep();
    window.setBackground(Color.GREEN);
    sleep();
    final Robot robot = new Robot();
    robot.setAutoDelay(200);
    Point point = window.getLocationOnScreen();
    Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
                                      point.y + window.getHeight() / 2);
    window.dispose();
    if (!color.equals(Color.GREEN)) {
        throw new RuntimeException(
                "Expected: " + Color.GREEN + " , Actual: " + color);
    }
}
 
Example 8
Source File: BackgroundIsNotUpdated.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static void main(final String[] args) throws AWTException {
    final Window window = new BackgroundIsNotUpdated(null);
    window.setSize(300, 300);
    window.setLocationRelativeTo(null);
    window.setVisible(true);
    sleep();
    window.setBackground(Color.GREEN);
    sleep();
    final Robot robot = new Robot();
    robot.setAutoDelay(200);
    Point point = window.getLocationOnScreen();
    Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
                                      point.y + window.getHeight() / 2);
    window.dispose();
    if (!color.equals(Color.GREEN)) {
        throw new RuntimeException(
                "Expected: " + Color.GREEN + " , Actual: " + color);
    }
}
 
Example 9
Source File: BackgroundIsNotUpdated.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(final String[] args) throws AWTException {
    final Window window = new BackgroundIsNotUpdated(null);
    window.setSize(300, 300);
    window.setLocationRelativeTo(null);
    window.setVisible(true);
    window.requestFocus();
    final ExtendedRobot robot = new ExtendedRobot();
    robot.setAutoDelay(200);
    robot.waitForIdle(1000);
    window.setBackground(Color.GREEN);
    robot.waitForIdle(1000);
    Point point = window.getLocationOnScreen();
    Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
                                      point.y + window.getHeight() / 2);
    window.dispose();
    if (!color.equals(Color.GREEN)) {
        throw new RuntimeException(
                "Expected: " + Color.GREEN + " , Actual: " + color);
    }
}
 
Example 10
Source File: BackgroundIsNotUpdated.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(final String[] args) throws AWTException {
    final Window window = new BackgroundIsNotUpdated(null);
    window.setSize(300, 300);
    window.setLocationRelativeTo(null);
    window.setVisible(true);
    sleep();
    window.setBackground(Color.GREEN);
    sleep();
    final Robot robot = new Robot();
    robot.setAutoDelay(200);
    Point point = window.getLocationOnScreen();
    Color color = robot.getPixelColor(point.x + window.getWidth() / 2,
                                      point.y + window.getHeight() / 2);
    window.dispose();
    if (!color.equals(Color.GREEN)) {
        throw new RuntimeException(
                "Expected: " + Color.GREEN + " , Actual: " + color);
    }
}
 
Example 11
Source File: PGTUtil.java    From PolyGlot with MIT License 5 votes vote down vote up
/**
 * Checks that the position is in bounds for the screen and places it in
 * visible area if not
 *
 * @param w
 */
public static void checkPositionInBounds(Window w) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Point location = w.getLocationOnScreen();

    // if this would appear offscreen, simply place it in the center of the screen
    if (screenSize.getWidth() < location.x || screenSize.getHeight() < location.y) {
        w.setLocationRelativeTo(null);
    }
}
 
Example 12
Source File: BaseService.java    From Shuffle-Move with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Given a window object, and a proposed x and y position on the screen, perform any necessary
 * correction to force it to remain on at least one of the available display devices. If the top
 * right corner of the window is not within any display device, it will be moved to the default
 * location.
 * 
 * @param w
 *           A Window that is moving, or about to be moved.
 * @param x
 *           The relative horizontal position to the display origin
 * @param y
 *           The relative vertical position to the display origin
 */
public static void correctPosition(Window w, int x, int y, ConfigManager pm) {
   boolean isMaximizedEither = false;
   if (w != null && Frame.class.isInstance(w)) {
      int state = ((Frame) w).getExtendedState();
      boolean maximizedHoriz = (state & JFrame.MAXIMIZED_HORIZ) != 0;
      boolean maximizedVert = (state & JFrame.MAXIMIZED_VERT) != 0;
      isMaximizedEither = maximizedHoriz || maximizedVert;
   }
   
   if (pm.getBooleanValue(KEY_WINDOW_SAFETY_ENABLED, true) && !isLocationInScreenBounds(x, y)
         && !isMaximizedEither) {
      w.setLocationRelativeTo(null);
   }
}
 
Example 13
Source File: DumpOnKey.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] args) throws AWTException {
    final boolean dump = Boolean.parseBoolean(args[0]);
    final Window w = new Frame() {
        @Override
        public void list(final PrintStream out, final int indent) {
            super.list(out, indent);
            dumped = true;
        }
    };
    w.setSize(200, 200);
    w.setLocationRelativeTo(null);
    w.setVisible(true);

    final Robot robot = new Robot();
    robot.setAutoDelay(50);
    robot.setAutoWaitForIdle(true);
    robot.mouseMove(w.getX() + w.getWidth() / 2,
                    w.getY() + w.getHeight() / 2);
    robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_SHIFT);
    robot.keyPress(KeyEvent.VK_F1);
    robot.keyRelease(KeyEvent.VK_F1);
    robot.keyRelease(KeyEvent.VK_SHIFT);
    robot.keyRelease(KeyEvent.VK_CONTROL);

    w.dispose();
    if (dumped != dump) {
        throw new RuntimeException("Exp:" + dump + ", actual:" + dumped);
    }
}
 
Example 14
Source File: ShowRowAction.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void actionPerformed(ActionEvent e) {
	Window dlg = getTable().getEditor(getRow());
	if (dlg != null) {
		if (dlg instanceof Frame) {
			((Frame) dlg).setState(Frame.NORMAL);
			((Frame) dlg).requestFocus();
		}
		dlg.setLocationRelativeTo(null);
		dlg.setAlwaysOnTop(true);
		dlg.setVisible(true);
	}
}
 
Example 15
Source File: WindowOwnedByEmbeddedFrameTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    try {
        Robot robot = Util.createRobot();
        robot.setAutoDelay(50);

        embeddedFrame = createEmbeddedFrame();

        textField = new TextField("");

        window = new Window(embeddedFrame);
        window.setSize(200, 200);
        window.setLocationRelativeTo(null);
        window.add(textField);
        window.setVisible(true);

        Util.waitForIdle(robot);

        Util.clickOnComp(textField, robot);
        Util.waitForIdle(robot);

        robot.keyPress(KeyEvent.VK_T);
        robot.keyRelease(KeyEvent.VK_T);
        Util.waitForIdle(robot);

        robot.keyPress(KeyEvent.VK_E);
        robot.keyRelease(KeyEvent.VK_E);
        Util.waitForIdle(robot);

        robot.keyPress(KeyEvent.VK_S);
        robot.keyRelease(KeyEvent.VK_S);
        Util.waitForIdle(robot);

        robot.keyPress(KeyEvent.VK_T);
        robot.keyRelease(KeyEvent.VK_T);
        Util.waitForIdle(robot);

        if ("".equals(textField.getText())) {
            throw new RuntimeException("Keyboard input in text field isn't possible");
        }
    } finally {
        if (embeddedFrame != null) {
            embeddedFrame.dispose();
        }
        if (window != null) {
            window.dispose();
        }
    }
}
 
Example 16
Source File: WindowOwnedByEmbeddedFrameTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    try {
        Robot robot = Util.createRobot();
        robot.setAutoDelay(50);

        embeddedFrame = createEmbeddedFrame();

        textField = new TextField("");

        window = new Window(embeddedFrame);
        window.setSize(200, 200);
        window.setLocationRelativeTo(null);
        window.add(textField);
        window.setVisible(true);

        Util.waitForIdle(robot);

        Util.clickOnComp(textField, robot);
        Util.waitForIdle(robot);

        robot.keyPress(KeyEvent.VK_T);
        robot.keyRelease(KeyEvent.VK_T);
        Util.waitForIdle(robot);

        robot.keyPress(KeyEvent.VK_E);
        robot.keyRelease(KeyEvent.VK_E);
        Util.waitForIdle(robot);

        robot.keyPress(KeyEvent.VK_S);
        robot.keyRelease(KeyEvent.VK_S);
        Util.waitForIdle(robot);

        robot.keyPress(KeyEvent.VK_T);
        robot.keyRelease(KeyEvent.VK_T);
        Util.waitForIdle(robot);

        if ("".equals(textField.getText())) {
            throw new RuntimeException("Keyboard input in text field isn't possible");
        }
    } finally {
        if (embeddedFrame != null) {
            embeddedFrame.dispose();
        }
        if (window != null) {
            window.dispose();
        }
    }
}
 
Example 17
Source File: WindowOwnedByEmbeddedFrameTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    try {
        Robot robot = Util.createRobot();
        robot.setAutoDelay(50);

        embeddedFrame = createEmbeddedFrame();

        textField = new TextField("");

        window = new Window(embeddedFrame);
        window.setSize(200, 200);
        window.setLocationRelativeTo(null);
        window.add(textField);
        window.setVisible(true);

        Util.waitForIdle(robot);

        Util.clickOnComp(textField, robot);
        Util.waitForIdle(robot);

        robot.keyPress(KeyEvent.VK_T);
        robot.keyRelease(KeyEvent.VK_T);
        Util.waitForIdle(robot);

        robot.keyPress(KeyEvent.VK_E);
        robot.keyRelease(KeyEvent.VK_E);
        Util.waitForIdle(robot);

        robot.keyPress(KeyEvent.VK_S);
        robot.keyRelease(KeyEvent.VK_S);
        Util.waitForIdle(robot);

        robot.keyPress(KeyEvent.VK_T);
        robot.keyRelease(KeyEvent.VK_T);
        Util.waitForIdle(robot);

        if ("".equals(textField.getText())) {
            throw new RuntimeException("Keyboard input in text field isn't possible");
        }
    } finally {
        if (embeddedFrame != null) {
            embeddedFrame.dispose();
        }
        if (window != null) {
            window.dispose();
        }
    }
}
 
Example 18
Source File: Frames.java    From chipster with MIT License 4 votes vote down vote up
public void setLocationRelativeToMainFrame(Window window) {
	window.setLocationRelativeTo(mainFrame);
}
 
Example 19
Source File: GuiHelper.java    From binnavi with Apache License 2.0 4 votes vote down vote up
public static final void centerOnScreen(final Window frame) {
  frame.setLocationRelativeTo(null);
}
 
Example 20
Source File: SharedUtils.java    From sc2gears with Apache License 2.0 2 votes vote down vote up
/**
 * Centers a window on its screen.
 * @param window window to be centered
 */
public static void centerWindow( final Window window ) {
	window.setLocationRelativeTo( null );
}