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:Main.java

public static void center(final Component c) {
    final Dimension screenDimensions = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (screenDimensions.width - c.getWidth()) / 2;
    int y = (screenDimensions.height - c.getHeight()) / 2;
    c.setLocation(x, y);
}

From source file:Main.java

public static void center(Component component) {
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension screenSize = toolkit.getScreenSize();
    int x = (screenSize.width - component.getWidth()) / 2;
    int y = (screenSize.height - component.getHeight()) / 2;
    component.setLocation(x, y);
}

From source file:Main.java

public static void centerComponent(Component relativeTo, Component toCenter) {
    if (relativeTo == null) {
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        toCenter.setLocation((screenWidth / 2) - (toCenter.getSize().width / 2),
                (screenHeight / 2) - (toCenter.getSize().height / 2));
    } else {/* ww  w  .  j  a va 2 s . c  o  m*/
        Point loc = relativeTo.getLocationOnScreen();
        Rectangle bounds = relativeTo.getBounds();
        toCenter.setLocation((int) (loc.x + bounds.getWidth() / 2) - (toCenter.getWidth() / 2),
                (int) (loc.y + bounds.getHeight() / 2) - (toCenter.getHeight() / 2));

    }
}

From source file:Main.java

/**
 * Centralizar componente// ww  w.ja  va  2  s  .  c  om
 * @author marcos
 * @param janela
 */
public static void centralizar(java.awt.Component janela) {
    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    java.awt.Rectangle b = janela.getBounds();
    int x, y;//height, width;

    x = (screenSize.width - b.width) / 2;
    y = (screenSize.height - b.height) / 2;
    janela.setLocation(x, y);
}

From source file:Main.java

static public void centerComponent(Component child, Point parentLocation, Dimension parentSize) {
    Dimension childSize = child.getSize();

    if (childSize.width > parentSize.width)
        childSize.width = parentSize.width;
    if (childSize.height > parentSize.height)
        childSize.height = parentSize.height;
    child.setLocation(parentLocation.x + parentSize.width / 2 - childSize.width / 2,
            parentLocation.y + parentSize.height / 2 - childSize.height / 2);
}

From source file:BooksDemo.java

public static void centerOnScreen(Component component) {
    Dimension paneSize = component.getSize();
    Dimension screenSize = component.getToolkit().getScreenSize();
    component.setLocation((screenSize.width - paneSize.width) / 2, (screenSize.height - paneSize.height) / 2);
}

From source file:Main.java

public static final void center(Component component) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = component.getSize();

    if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
    }//from w  w  w  .  j a v a  2  s  . c  o m

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

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

From source file:Main.java

public static final void fullScreen(Component component) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = component.getSize();

    if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
    }//from   ww  w .j  a  va2 s .  c  om

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

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

From source file:Main.java

private static void center(final Component wind, final Rectangle rect) {
    if ((wind == null) || (rect == null)) {
        return;//w  w  w.j av a2 s .  c o  m
    }
    final Dimension windSize = wind.getSize();
    final int x = ((rect.width - windSize.width) / 2) + rect.x;
    int y = ((rect.height - windSize.height) / 2) + rect.y;
    if (y < rect.y) {
        y = rect.y;
    }
    wind.setLocation(x, y);
}

From source file:Main.java

/**
 * Centers a component on screen.//www . j av a 2  s .c  om
 *
 * @param c The component that will be centered.
 */
public static void centerComponentOnScreen(Component c) {

    // Size of the screen.
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    double width = screenSize.getWidth();
    double height = screenSize.getHeight();

    // Size of the component.
    Dimension size = c.getSize();

    width -= size.getWidth();
    height -= size.getHeight();

    c.setLocation((int) width / 2, (int) height / 2);
}