Example usage for java.awt Component setLocation

List of usage examples for java.awt Component setLocation

Introduction

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

Prototype

public void setLocation(int x, int y) 

Source Link

Document

Moves this component to a new location.

Usage

From source file:org.openmicroscopy.shoola.util.ui.UIUtilities.java

/**
 * Centers the specified component on the screen.
 * The location of the specified component is set so that it will appear
 * in the middle of the screen when made visible.
 * This method is mainly useful for windows, frames and dialogs.
 * //from w  ww  .j  av  a 2  s .  c om
 * @param window   The component to center.
 */
public static void centerOnScreen(Component window) {
    if (window == null)
        return;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Rectangle ed = window.getBounds();
    window.setLocation((screenSize.width - ed.width) / 2, (screenSize.height - ed.height) / 2);
}

From source file:org.openmicroscopy.shoola.util.ui.UIUtilities.java

/**
 * Sets the location of the specified child relative to the location
 * of the specified parent and then makes it visible.
 * This method is mainly useful for windows, frames and dialogs. 
 * /*from  w ww  .ja  v  a 2  s  .c  om*/
 * @param parent    The visible parent.
 * @param child     The child to display.
 */
public static void setLocationRelativeTo(Component parent, Component child) {
    if (parent == null || child == null)
        return;
    int x = parent.getX() + parent.getWidth();
    int y = parent.getY();
    int childWidth = child.getWidth();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    if (x + childWidth > screenSize.getWidth()) {
        if (childWidth < parent.getX())
            x = parent.getX() - childWidth;
        else
            x = (int) (screenSize.getWidth() - childWidth);
    }
    child.setLocation(x, y);
    child.setVisible(true);
}

From source file:org.openmicroscopy.shoola.util.ui.UIUtilities.java

/**
 * Sets the location of the specified child relative to the passed 
 * bounds.//from w  w w.  j ava2s . c  o  m
 * This method is mainly useful for windows, frames and dialogs. 
 * 
 * @param parentBounds  The bounds of the parent.
 * @param child        The child to display.
 */
public static void setLocationRelativeTo(Rectangle parentBounds, Component child) {
    if (child == null)
        return;
    if (parentBounds == null)
        parentBounds = new Rectangle(0, 0, 5, 5);

    int x = parentBounds.x + parentBounds.width;
    int y = parentBounds.y;
    int childWidth = child.getWidth();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    if (x + childWidth > screenSize.getWidth()) {
        if (childWidth < parentBounds.x)
            x = parentBounds.x - childWidth;
        else
            x = (int) (screenSize.getWidth() - childWidth);
    }
    child.setLocation(x, y);
    child.setVisible(true);
}

From source file:org.openmicroscopy.shoola.util.ui.UIUtilities.java

/**
 * Sets the location of the specified child relative to the location
 * of the specified parent and then makes it visible, and size to fill window.
 * This method is mainly useful for windows, frames and dialogs. 
 * //from w  w  w .ja v  a 2s .c  o  m
 * @param parentBounds    The bounds of the visible parent.
 * @param child     The child to display.
 * @param max      The maximum size of the window.
 */
public static void setLocationRelativeToAndSizeToWindow(Rectangle parentBounds, Component child,
        Dimension max) {
    if (child == null)
        return;
    if (parentBounds == null)
        parentBounds = new Rectangle(0, 0, 5, 5);
    if (max == null)
        max = new Dimension(5, 5);
    int x = (int) (parentBounds.getX() + parentBounds.getWidth());
    int y = (int) parentBounds.getY();
    int childWidth = child.getWidth();
    int childHeight = child.getHeight();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    if (x + childWidth > screenSize.getWidth()) {
        if (childWidth < parentBounds.getX())
            x = (int) (parentBounds.getX()) - childWidth;
        else
            x = (int) (screenSize.getWidth() - childWidth);
    }
    child.setLocation(x, y);
    int newHeight = (int) screenSize.getHeight() - y - 10;
    int newWidth = (int) screenSize.getWidth() - x - 10;

    if (newWidth > childWidth)
        childWidth = newWidth;
    if (newHeight > childHeight)
        childHeight = newHeight;

    if (childWidth > max.getWidth())
        childWidth = (int) max.getWidth();
    if (childHeight > max.getHeight())
        childHeight = (int) max.getHeight();

    child.setSize(childWidth, childHeight);
    child.setVisible(true);
}