Example usage for java.awt Component getPreferredSize

List of usage examples for java.awt Component getPreferredSize

Introduction

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

Prototype

public Dimension getPreferredSize() 

Source Link

Document

Gets the preferred size of this component.

Usage

From source file:RelativeLayout.java

/**
 * Called by AWT to lay out the components in the target Container at its
 * current size./*  w w  w .ja v a  2s .  c  o  m*/
 * 
 * @param target
 *            Container whose components are to be laid out.
 */
public void layoutContainer(Container target) {
    Dimension targSize = target.getSize();
    Insets ins = target.getInsets();
    // System.out.println("layoutContainer: size " + targSize);
    curWid = targSize.width;
    curHgt = targSize.height;
    float widRatio = (float) curWid / (float) reqWid;
    float hgtRatio = (float) curHgt / (float) reqHgt;
    for (int i = 0; i < curComps.size(); i++) {
        int px, py, pw, ph;
        Tracker t = (Tracker) curComps.elementAt(i);
        Component tc = t.getComponent();
        Dimension d = tc.getPreferredSize();
        px = ins.right + (int) (t.getRequestedLoc().x * widRatio);
        py = ins.top + (int) (t.getRequestedLoc().y * hgtRatio);
        pw = d.width;
        ph = d.height;
        // System.out.println("layoutContainer["+i+"]: move " +
        // tc + " to " + px + ", " + py);
        tc.setBounds(px, py, pw, ph);
    }
}

From source file:edu.uci.ics.jung.visualization.renderers.VertexLabelAsShapeRenderer.java

public Shape transform(V v) {
    Component component = prepareRenderer(rc, rc.getVertexLabelRenderer(),
            rc.getVertexLabelTransformer().transform(v), rc.getPickedVertexState().isPicked(v), v);
    Dimension size = component.getPreferredSize();
    Rectangle bounds = new Rectangle(-size.width / 2 - 2, -size.height / 2 - 2, size.width + 4, size.height);
    return bounds;
    //      Shape shape = shapes.get(v);
    //      if(shape == null) {
    //         return new Rectangle(-20,-20,40,40);
    //      }//from   ww w  .j  av a  2  s .  c o m
    //      else return shape;
}

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

private Dimension layoutSize(Container parent, boolean minimum) {
    Dimension dim = new Dimension(0, 0);
    synchronized (parent.getTreeLock()) {
        int n = parent.getComponentCount();
        int cnt = 0;
        for (int i = 0; i < n; i++) {
            Component c = parent.getComponent(i);
            int maxhgt = 0;
            if (c.isVisible()) {
                Dimension d = (minimum) ? c.getMinimumSize() : c.getPreferredSize();
                dim.width += d.width;//from  www . j a v a  2 s.co  m
                if (d.height > dim.height)
                    dim.height = d.height;
            }
            cnt++;
            if (cnt == num)
                break;
        }
    }
    Insets insets = parent.getInsets();
    dim.width += insets.left + insets.right;
    dim.height += insets.top + insets.bottom;
    return dim;
}

From source file:ca.ubc.cs.ferret.cg.VertexLabelAsShapeRenderer.java

public Shape transform(V v) {
    Component component = prepareRenderer(rc, rc.getVertexLabelRenderer(),
            rc.getVertexLabelTransformer().transform(v), rc.getPickedVertexState().isPicked(v), v);
    Dimension size = component.getPreferredSize();
    Rectangle bounds = new Rectangle(-size.width / 2 - 2, -size.height / 2 - 4, size.width + 4,
            size.height + 6);//from   w w  w  .jav a  2 s.c  om
    return bounds;
    //      Shape shape = shapes.get(v);
    //      if(shape == null) {
    //         return new Rectangle(-20,-20,40,40);
    //      }
    //      else return shape;
}

From source file:VerticalLayout.java

private Dimension layoutSize(Container parent, boolean minimum) {
    Dimension dim = new Dimension(0, 0);
    Dimension d;/*  w  ww .j  av a2 s.c  om*/
    synchronized (parent.getTreeLock()) {
        int n = parent.getComponentCount();
        for (int i = 0; i < n; i++) {
            Component c = parent.getComponent(i);
            if (c.isVisible()) {
                d = minimum ? c.getMinimumSize() : c.getPreferredSize();
                dim.width = Math.max(dim.width, d.width);
                dim.height += d.height;
                if (i > 0)
                    dim.height += vgap;
            }
        }
    }
    Insets insets = parent.getInsets();
    dim.width += insets.left + insets.right;
    dim.height += insets.top + insets.bottom + vgap + vgap;
    return dim;
}

From source file:CircleLayoutDemo.java

/**
 * Arranges the parent's Component objects in either an Ellipse or a Circle.
 * Ellipse is not yet implemented.//w  ww  . j  ava2 s .  c  o  m
 */
public void layoutContainer(Container parent) {
    int x, y, w, h, s, c;
    int n = parent.getComponentCount();
    double parentWidth = parent.getSize().width;
    double parentHeight = parent.getSize().height;
    Insets insets = parent.getInsets();
    int centerX = (int) (parentWidth - (insets.left + insets.right)) / 2;
    int centerY = (int) (parentHeight - (insets.top + insets.bottom)) / 2;

    Component comp = null;
    Dimension compPS = null;
    if (n == 1) {
        comp = parent.getComponent(0);
        x = centerX;
        y = centerY;
        compPS = comp.getPreferredSize();
        w = compPS.width;
        h = compPS.height;
        comp.setBounds(x, y, w, h);
    } else {
        double r = (Math.min(parentWidth - (insets.left + insets.right),
                parentHeight - (insets.top + insets.bottom))) / 2;
        r *= 0.75; // Multiply by .75 to account for extreme right and bottom
                   // Components
        for (int i = 0; i < n; i++) {
            comp = parent.getComponent(i);
            compPS = comp.getPreferredSize();
            if (isCircle) {
                c = (int) (r * Math.cos(2 * i * Math.PI / n));
                s = (int) (r * Math.sin(2 * i * Math.PI / n));
            } else {
                c = (int) ((centerX * 0.75) * Math.cos(2 * i * Math.PI / n));
                s = (int) ((centerY * 0.75) * Math.sin(2 * i * Math.PI / n));
            }
            x = c + centerX;
            y = s + centerY;

            w = compPS.width;
            h = compPS.height;

            comp.setBounds(x, y, w, h);
        }
    }

}

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 .  ja v a  2  s  . c  o  m*/
    }

    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:de.fub.maps.gpx.analysis.ui.GpxTrkSegAnalysizerTopComponent.java

public GpxTrkSegAnalysizerTopComponent() {
    initComponents();//from  w  ww.  j a  v a 2  s  . c  o  m
    setName(NbBundle.getMessage(GpxTrkSegAnalysizerTopComponent.class, "CTL_GpxTrkSegAnalysizerTopComponent"));
    setToolTipText(
            NbBundle.getMessage(GpxTrkSegAnalysizerTopComponent.class, "HINT_GpxTrkSegAnalysizerTopComponent"));
    jSplitPane1.setDividerLocation(.75);

    int height = 0;
    for (Component child : jPanel3.getComponents()) {
        height += child.getPreferredSize().height;
    }
    jScrollPane1.getViewport().getView().setPreferredSize(new Dimension(0, height));

    addComponentListener(new ComponentAdapterImpl());
    init();
}

From source file:RadialLayout.java

/**
 * Sets the sizes attribute of the RadialLayout object.
 *
 * @param  parent  the parent.//  www.ja  v  a 2 s.  com
 * 
 * @see LayoutManager
 */
private void setSizes(final Container parent) {
    final int nComps = parent.getComponentCount();
    //Reset preferred/minimum width and height.
    this.preferredWidth = 0;
    this.preferredHeight = 0;
    this.minWidth = 0;
    this.minHeight = 0;
    for (int i = 0; i < nComps; i++) {
        final Component c = parent.getComponent(i);
        if (c.isVisible()) {
            final Dimension d = c.getPreferredSize();
            if (this.maxCompWidth < d.width) {
                this.maxCompWidth = d.width;
            }
            if (this.maxCompHeight < d.height) {
                this.maxCompHeight = d.height;
            }
            this.preferredWidth += d.width;
            this.preferredHeight += d.height;
        }
    }
    this.preferredWidth = this.preferredWidth / 2;
    this.preferredHeight = this.preferredHeight / 2;
    this.minWidth = this.preferredWidth;
    this.minHeight = this.preferredHeight;
}

From source file:ColumnLayout.java

protected Dimension layoutSize(Container parent, int sizetype) {
    int nkids = parent.getComponentCount();
    Dimension size = new Dimension(0, 0);
    Insets insets = parent.getInsets();
    int num_visible_kids = 0;

    // Compute maximum width and total height of all visible kids
    for (int i = 0; i < nkids; i++) {
        Component kid = parent.getComponent(i);
        Dimension d;//from   www  .j  a v  a 2  s . co m
        if (!kid.isVisible())
            continue;
        num_visible_kids++;
        if (sizetype == 1)
            d = kid.getPreferredSize();
        else if (sizetype == 2)
            d = kid.getMinimumSize();
        else
            d = kid.getMaximumSize();
        if (d.width > size.width)
            size.width = d.width;
        size.height += d.height;
    }

    // Now add in margins and stuff
    size.width += insets.left + insets.right + 2 * margin_width;
    size.height += insets.top + insets.bottom + 2 * margin_height;
    if (num_visible_kids > 1)
        size.height += (num_visible_kids - 1) * spacing;
    return size;
}