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

/**
 * Use this static method if you want to center a component over another
 * component.//ww  w.  jav a2  s. c o m
 *
 * @param parent
 *            the component you want to use to place it on
 * @param toBeCentered
 *            the component you want to center
 */
public static void centerComponentInComponent(Component parent, Component toBeCentered) {
    toBeCentered.setLocation(parent.getX() + (parent.getWidth() - toBeCentered.getWidth()) / 2,
            parent.getY() + (parent.getHeight() - toBeCentered.getHeight()) / 2);

    toBeCentered.validate();
    toBeCentered.repaint();
}

From source file:Main.java

/**
 * Centers component <code>b</code> within component <code>a</code>.
 *//*from  w w  w  .j a  v  a2  s  . com*/
public static void centerComponent(Component a, Component b) {
    Dimension asize = a.getSize(), bsize = b.getSize();
    b.setLocation((asize.width - bsize.width) / 2, (asize.height - bsize.height) / 2);
}

From source file:Main.java

private static void doSetLocationInEDT(final Component component, final int x, final int y) {
    component.setLocation(x, y);
}

From source file:Main.java

/**
 * Centers the component on the screen//from   ww  w  .  ja  v  a  2 s.c  o m
 */
public static void centerOnScreen(Component comp) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    comp.setLocation((screenSize.width - comp.getWidth()) / 2, (screenSize.height - comp.getHeight()) / 2);
}

From source file:Main.java

public static void centerOnScreen(Component aCompo, Dimension aCompoSize) {
    final Dimension lScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
    aCompo.setLocation(lScreenSize.width / 2 - (aCompoSize.width / 2),
            lScreenSize.height / 2 - (aCompoSize.height / 2));
}

From source file:Main.java

/**
 * Use this static method if you want to center a component in Window.
 *
 * @param component//ww  w .ja  v a 2 s.  com
 *            the component you want to center in window
 */
public static void centerComponentInWindow(Component component) {
    Dimension dimension = component.getToolkit().getScreenSize();

    component.setLocation((int) ((dimension.getWidth() - component.getWidth()) / 2),
            (int) ((dimension.getHeight() - component.getHeight()) / 2));
    component.validate();
    component.repaint();
}

From source file:com.cats.version.utils.Utils.java

public static void centerWindow(int width, int height, Component ui) {
    ui.setLocation((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - width) / 2,
            (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - height) / 2);
}

From source file:Main.java

/**
 * Position the given component at the center of the given parent component or physical screen.
 * /*from  w w w  .j  a v a2s  .  c om*/
 * @param c the component to be positioned
 * @param parent the component whose center will match the center of the given component.
 * If null, the given component will match the screen center.
 * 
 */
public static void position(Component c, Component parent) {
    Dimension d = c.getPreferredSize();
    if (parent == null) {
        Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
        c.setLocation(s.width / 2 - d.width / 2, s.height / 2 - d.height / 2);
    } else {
        Point p = parent.getLocationOnScreen();
        int pw = parent.getWidth();
        int ph = parent.getHeight();
        c.setLocation(p.x + pw / 2 - d.width / 2, p.y + ph / 2 - d.height / 2);
    }
}

From source file:Main.java

/**
 * Gauge the given component on screen.// w  w  w. jav a2 s  .  com
 *
 * @param component Swing-component to gauge.
 */
public static void makeCentered(final Component component) {
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension dim = toolkit.getScreenSize();
    int screenWidth = dim.width;
    int screenHeight = dim.height;
    component.setLocation((screenWidth - component.getSize().width) / 2,
            (screenHeight - component.getSize().height) / 2);
}

From source file:Main.java

public static void setLocation(final Component component, final int x, final int y) {
    if (component != null && x >= 0 && y >= 0) {
        runInEDT(() -> component.setLocation(x, y));
    }/*from   w  w  w.ja  v  a 2s  . c  om*/
}