Example usage for java.awt Component setBounds

List of usage examples for java.awt Component setBounds

Introduction

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

Prototype

public void setBounds(int x, int y, int width, int height) 

Source Link

Document

Moves and resizes this component.

Usage

From source file:Main.java

public static void centerOnScreen(Component component, int width, int height) {

    int x = getScreenWidth() / 2 - width / 2;
    int y = getScreenHeight() / 2 - height / 2;

    component.setBounds(x, y, width, height);
}

From source file:Main.java

/**
 * Centers a component according to the window location.
 * @param wnd The parent window/*  w w  w.j a  v a2  s .  c om*/
 * @param cmp A component, usually a dialog
 */
public static void centerInWindow(Window wnd, Component cmp) {
    Dimension size = wnd.getSize();
    Point loc = wnd.getLocationOnScreen();
    Dimension cmpSize = cmp.getSize();
    loc.x += (size.width - cmpSize.width) / 2;
    loc.y += (size.height - cmpSize.height) / 2;
    cmp.setBounds(loc.x, loc.y, cmpSize.width, cmpSize.height);
}

From source file:WrapperLayout.java

public void layoutContainer(Container arg0) {
    int count = arg0.getComponentCount();
    if (count > 0) {
        Component child = arg0.getComponent(0);
        java.awt.Insets insets = arg0.getInsets();
        child.setBounds(insets.left, insets.top, arg0.getWidth() - insets.left - insets.right,
                arg0.getHeight() - insets.top - insets.bottom);
    }//  www. jav a2  s  .  c o  m
}

From source file:com.equitysoft.cellspark.ProportionalLayout.java

/**
 * Lays out the container.//ww w  .ja  va 2 s  .  co  m
 */
public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    synchronized (parent.getTreeLock()) {
        int n = parent.getComponentCount();
        Dimension pd = parent.getSize();
        //do layout
        int cnt = 0;
        int totalwid = pd.width - insets.left - insets.right;
        int x = insets.left;
        int cumwid = 0;
        for (int i = 0; i < n; i++) {
            Component c = parent.getComponent(i);
            int wid = proportions[i] * totalwid / total;
            c.setBounds(x, insets.top, wid, pd.height - insets.bottom - insets.top);
            x += wid;
            cnt++;
            if (cnt == num)
                break;
        }
    }
}

From source file:FormLayoutTester.java

public void layoutContainer(Container parent) {
    preferredLayoutSize(parent); // Sets left, right

    Component[] components = parent.getComponents();

    Insets insets = parent.getInsets();
    int xcenter = insets.left + left;
    int y = insets.top;

    for (int i = 0; i < components.length; i += 2) {
        Component cleft = components[i];
        Component cright = components[i + 1];

        Dimension dleft = cleft.getPreferredSize();
        Dimension dright = cright.getPreferredSize();

        int height = Math.max(dleft.height, dright.height);

        cleft.setBounds(xcenter - dleft.width, y + (height - dleft.height) / 2, dleft.width, dleft.height);

        cright.setBounds(xcenter + GAP, y + (height - dright.height) / 2, dright.width, dright.height);
        y += height;/*from  w  w  w.j  a va2 s  . com*/
    }
}

From source file:com.quinsoft.zeidon.objectbrowser.WindowBoundsRestorer.java

private void setBounds(String key, Component c) {
    key = key + c.getName();// w  w w.  j av a 2 s.  c om

    String position = properties.getProperty(key);
    if (c.getName() != null && !StringUtils.isBlank(position)) {
        String[] nums = position.split(",");
        c.setBounds(Integer.parseInt(nums[0]), Integer.parseInt(nums[1]), Integer.parseInt(nums[2]),
                Integer.parseInt(nums[3]));
    }

    if (c instanceof Container) {
        key = key + "/";
        Container container = (Container) c;
        for (Component child : container.getComponents())
            setBounds(key, child);
    }
}

From source file:OverlayLayout.java

/**
 * Lays out the specified container.// ww  w  .j  av a 2 s  . c  om
 *
 * @param parent the container to be laid out
 */
public void layoutContainer(final Container parent) {
    synchronized (parent.getTreeLock()) {
        final Insets ins = parent.getInsets();

        final Rectangle bounds = parent.getBounds();
        final int width = bounds.width - ins.left - ins.right;
        final int height = bounds.height - ins.top - ins.bottom;

        final Component[] comps = parent.getComponents();

        for (int i = 0; i < comps.length; i++) {
            final Component c = comps[i];
            if ((comps[i].isVisible() == false) && this.ignoreInvisible) {
                continue;
            }
            c.setBounds(ins.left, ins.top, width, height);
        }
    }
}

From source file:StackLayout.java

public void layoutContainer(Container parent) {
    Component visibleComp = getVisibleComponent();
    if (visibleComp != null) {
        synchronized (parent.getTreeLock()) {
            Insets insets = parent.getInsets();
            visibleComp.setBounds(insets.left, insets.top, parent.getWidth() - (insets.left + insets.right),
                    parent.getHeight() - (insets.top + insets.bottom));
        }/*from   ww  w  .  j  a v  a  2s  . c  o  m*/
    }
}

From source file:XYLayout.java

public void layoutContainer(Container target) {
    Insets insets = target.getInsets();
    int count = target.getComponentCount();
    for (int i = 0; i < count; i++) {
        Component component = target.getComponent(i);
        if (component.isVisible()) {
            Rectangle r = getComponentBounds(component, true);
            component.setBounds(insets.left + r.x, insets.top + r.y, r.width, r.height);
        }/*w  w  w .j  ava2  s . co  m*/
    }

}

From source file:CircleLayoutTest.java

public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    int containerWidth = parent.getSize().width - insets.left - insets.right;
    int containerHeight = parent.getSize().height - insets.top - insets.bottom;
    int xradius = (containerWidth - maxComponentWidth) / 2;
    int yradius = (containerHeight - maxComponentHeight) / 2;

    setSizes(parent);//w w w. j  a  va 2  s  . c o m
    int centerX = insets.left + containerWidth / 2;
    int centerY = insets.top + containerHeight / 2;

    int comCount = parent.getComponentCount();
    for (int i = 0; i < comCount; i++) {
        Component c = parent.getComponent(i);
        if (c.isVisible()) {
            Dimension size = c.getPreferredSize();
            double angle = 2 * Math.PI * i / comCount;
            int x = centerX + (int) (Math.cos(angle) * xradius);
            int y = centerY + (int) (Math.sin(angle) * yradius);

            c.setBounds(x - size.width / 2, y - size.height / 2, size.width, size.height);
        }
    }
}