Example usage for java.awt Window setLocationRelativeTo

List of usage examples for java.awt Window setLocationRelativeTo

Introduction

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

Prototype

public void setLocationRelativeTo(Component c) 

Source Link

Document

Sets the location of the window relative to the specified component according to the following scenarios.

Usage

From source file:Main.java

/**
 * Sets the given {@link Window} to the center of the sceen
 * /*ww w.  j  ava2  s  .  com*/
 * @param window to set to the screen center
 */
public static void setLocationToCenter(Window window) {
    window.setLocationRelativeTo(null);
}

From source file:Main.java

/**
 * Centers the given {@link Window} on the screen using
 * {@link Window#setLocationRelativeTo(Component)}.
 * /*from   ww w .  j  a v a2s . c om*/
 * @param frame
 *            - the window to center
 */
public static void drop(Window frame) {
    frame.setLocationRelativeTo(null);
}

From source file:Main.java

/**
 * A version of setLocationRelativeTo() that takes the maximum window bounds into account
 * (taskbar).// ww w.j  a  v  a 2 s . co m
 */
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);
}

From source file:Main.java

public static void packLater(final Window win, final Component parent) {
    win.pack();//from   w  w  w .  j a v  a 2  s.c  o m
    win.setLocationRelativeTo(parent);
    win.addWindowListener(new WindowAdapter() {
        @Override
        public void windowOpened(WindowEvent e) {
            win.pack();
            win.setLocationRelativeTo(parent);
        }
    });
}

From source file:Main.java

/**
 * Center window on screen.//  w ww .  ja v a2 s  . co 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(final Window window, final 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();
    }

    window.setLocationRelativeTo(null);
}

From source file:processing.app.Base.java

/**
 * Show the About box.//  www.j  a v a 2  s. c om
 */
@SuppressWarnings("serial")
public void handleAbout() {
    final Image image = Theme.getLibImage("about", activeEditor, Theme.scale(475), Theme.scale(300));
    final Window window = new Window(activeEditor) {
        public void paint(Graphics graphics) {
            Graphics2D g = Theme.setupGraphics2D(graphics);
            g.drawImage(image, 0, 0, null);

            Font f = new Font("SansSerif", Font.PLAIN, Theme.scale(11));
            g.setFont(f);
            g.setColor(new Color(0, 151, 156));
            g.drawString(BaseNoGui.VERSION_NAME_LONG, Theme.scale(33), Theme.scale(20));
        }
    };
    window.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            window.dispose();
        }
    });
    int w = image.getWidth(activeEditor);
    int h = image.getHeight(activeEditor);
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    window.setBounds((screen.width - w) / 2, (screen.height - h) / 2, w, h);
    window.setLocationRelativeTo(activeEditor);
    window.setVisible(true);
}