Example usage for java.awt Container getInsets

List of usage examples for java.awt Container getInsets

Introduction

In this page you can find the example usage for java.awt Container getInsets.

Prototype

public Insets getInsets() 

Source Link

Document

Determines the insets of this container, which indicate the size of the container's border.

Usage

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;/*www .j  a va 2  s .co  m*/
    }
}

From source file:ColumnLayout.java

/**
 * The method that actually performs the layout. Called by the Container
 *//*  w w  w.  j a v a  2s.c  o  m*/
public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    Dimension parent_size = parent.getSize();
    Component kid;
    int nkids = parent.getComponentCount();
    int x0 = insets.left + margin_width; // The base X position
    int x;
    int y = insets.top + margin_height; // Start at the top of the column

    for (int i = 0; i < nkids; i++) { // Loop through the kids
        kid = parent.getComponent(i); // Get the kid
        if (!kid.isVisible())
            continue; // Skip hidden ones
        Dimension pref = kid.getPreferredSize(); // How big is it?
        switch (alignment) { // Compute X coordinate
        default:
        case LEFT:
            x = x0;
            break;
        case CENTER:
            x = (parent_size.width - pref.width) / 2;
            break;
        case RIGHT:
            x = parent_size.width - insets.right - margin_width - pref.width;
            break;
        }
        // Set the size and position of this kid
        kid.setBounds(x, y, pref.width, pref.height);
        y += pref.height + spacing; // Get Y position of the next one
    }
}

From source file:CircleLayoutTest.java

public void layoutContainer(Container parent) {
    setSizes(parent);/*from  w  w w. j a v  a 2  s  .c  o m*/

    // compute center of the circle

    Insets insets = parent.getInsets();
    int containerWidth = parent.getSize().width - insets.left - insets.right;
    int containerHeight = parent.getSize().height - insets.top - insets.bottom;

    int xcenter = insets.left + containerWidth / 2;
    int ycenter = insets.top + containerHeight / 2;

    // compute radius of the circle

    int xradius = (containerWidth - maxComponentWidth) / 2;
    int yradius = (containerHeight - maxComponentHeight) / 2;
    int radius = Math.min(xradius, yradius);

    // lay out components along the circle

    int n = parent.getComponentCount();
    for (int i = 0; i < n; i++) {
        Component c = parent.getComponent(i);
        if (c.isVisible()) {
            double angle = 2 * Math.PI * i / n;

            // center point of component
            int x = xcenter + (int) (Math.cos(angle) * radius);
            int y = ycenter + (int) (Math.sin(angle) * radius);

            // move component so that its center is (x, y)
            // and its size is its preferred size
            Dimension d = c.getPreferredSize();
            c.setBounds(x - d.width / 2, y - d.height / 2, d.width, d.height);
        }
    }
}

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);
        }//www . ja  v  a 2  s.c om
    }

}

From source file:VerticalLayout.java

/**
 * Lays out the container./*from w  w  w  .j  av a 2  s.co m*/
 */
public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    synchronized (parent.getTreeLock()) {
        int n = parent.getComponentCount();
        Dimension pd = parent.getSize();
        int y = 0;
        //work out the total size
        for (int i = 0; i < n; i++) {
            Component c = parent.getComponent(i);
            Dimension d = c.getPreferredSize();
            y += d.height + vgap;
        }
        y -= vgap; //otherwise there's a vgap too many
        //Work out the anchor paint
        if (anchor == TOP)
            y = insets.top;
        else if (anchor == CENTER)
            y = (pd.height - y) / 2;
        else
            y = pd.height - y - insets.bottom;
        //do layout
        for (int i = 0; i < n; i++) {
            Component c = parent.getComponent(i);
            Dimension d = c.getPreferredSize();
            int x = insets.left;
            int wid = d.width;
            if (alignment == CENTER)
                x = (pd.width - d.width) / 2;
            else if (alignment == RIGHT)
                x = pd.width - d.width - insets.right;
            else if (alignment == BOTH)
                wid = pd.width - insets.left - insets.right;
            c.setBounds(x, y, wid, d.height);
            y += d.height + vgap;
        }
    }
}

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);
    }/*from  w w w.  j  a  v a 2  s . c o  m*/
}

From source file:Main.java

public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    int maxWidth = parent.getWidth() - (insets.left + insets.right);
    int maxHeight = parent.getHeight() - (insets.top + insets.bottom);
    int nComps = parent.getComponentCount();
    int previousWidth = 0, previousHeight = 0;
    int x = 0, y = insets.top;
    int rowh = 0, start = 0;
    int xFudge = 0, yFudge = 0;
    boolean oneColumn = false;

    // Go through the components' sizes, if neither
    // preferredLayoutSize nor minimumLayoutSize has
    // been called.
    if (sizeUnknown) {
        setSizes(parent);/*w w  w. j a  va  2s .  c om*/
    }

    if (maxWidth <= minWidth) {
        oneColumn = true;
    }

    if (maxWidth != preferredWidth) {
        xFudge = (maxWidth - preferredWidth) / (nComps - 1);
    }

    if (maxHeight > preferredHeight) {
        yFudge = (maxHeight - preferredHeight) / (nComps - 1);
    }

    for (int i = 0; i < nComps; i++) {
        Component c = parent.getComponent(i);
        if (c.isVisible()) {
            Dimension d = c.getPreferredSize();

            // increase x and y, if appropriate
            if (i > 0) {
                if (!oneColumn) {
                    x += previousWidth / 2 + xFudge;
                }
                y += previousHeight + vgap + yFudge;
            }

            // If x is too large,
            if ((!oneColumn) && (x + d.width) > (parent.getWidth() - insets.right)) {
                // reduce x to a reasonable number.
                x = parent.getWidth() - insets.bottom - d.width;
            }

            // If y is too large,
            if ((y + d.height) > (parent.getHeight() - insets.bottom)) {
                // do nothing.
                // Another choice would be to do what we do to x.
            }

            // Set the component's size and position.
            c.setBounds(x, y, d.width, d.height);

            previousWidth = d.width;
            previousHeight = d.height;
        }
    }
}

From source file:CenterLayout.java

/**
 * Returns the minimum size.//  w ww. j  av a 2  s  .co m
 * 
 * @param parent
 *          the parent.
 * 
 * @return the minimum size.
 */
public Dimension minimumLayoutSize(final Container parent) {

    synchronized (parent.getTreeLock()) {
        final Insets insets = parent.getInsets();
        if (parent.getComponentCount() > 0) {
            final Component component = parent.getComponent(0);
            final Dimension d = component.getMinimumSize();
            return new Dimension(d.width + insets.left + insets.right, d.height + insets.top + insets.bottom);
        } else {
            return new Dimension(insets.left + insets.right, insets.top + insets.bottom);
        }
    }

}

From source file:CenterLayout.java

/**
 * Returns the preferred size.//ww w  .ja  va2s .c om
 * 
 * @param parent
 *          the parent.
 * 
 * @return the preferred size.
 */
public Dimension preferredLayoutSize(final Container parent) {

    synchronized (parent.getTreeLock()) {
        final Insets insets = parent.getInsets();
        if (parent.getComponentCount() > 0) {
            final Component component = parent.getComponent(0);
            final Dimension d = component.getPreferredSize();
            return new Dimension((int) d.getWidth() + insets.left + insets.right,
                    (int) d.getHeight() + insets.top + insets.bottom);
        } else {
            return new Dimension(insets.left + insets.right, insets.top + insets.bottom);
        }
    }

}

From source file:OverlayLayout.java

/**
 * Lays out the specified container./*from   w  ww . j  a  v  a  2s . 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);
        }
    }
}