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:edu.ku.brc.specify.plugins.sgr.SGRResultsDisplay.java

/**
 * From http://www.pikopong.com/blog/2008/08/13/auto-resize-jtable-column-width/
 * //from w w w  . j  ava2  s.  co m
 * @param table
 * @param model
 * @return
 */
private JTable autoResizeColWidth(JTable table, DefaultTableModel model) {
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setModel(model);

    int margin = 5;

    for (int i = 0; i < table.getColumnCount(); i++) {
        int vColIndex = i;
        DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
        TableColumn col = colModel.getColumn(vColIndex);
        int width = 0;

        // Get width of column header
        TableCellRenderer renderer = col.getHeaderRenderer();

        if (renderer == null) {
            renderer = table.getTableHeader().getDefaultRenderer();
        }

        Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0,
                0);

        width = comp.getPreferredSize().width;

        // Get maximum width of column data
        for (int r = 0; r < table.getRowCount(); r++) {
            renderer = table.getCellRenderer(r, vColIndex);
            comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false,
                    r, vColIndex);
            width = Math.max(width, comp.getPreferredSize().width);
        }

        // Add margin
        width += 2 * margin;

        // Set the width
        col.setPreferredWidth(width);
    }

    ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer())
            .setHorizontalAlignment(SwingConstants.LEFT);

    // table.setAutoCreateRowSorter(true);
    //table.getTableHeader().setReorderingAllowed(false);

    return table;
}

From source file:VerticalFlowLayout.java

/**
 *  Description of the Method//from  w ww. ja va2  s  .  c o  m
 *
 *@param  target  Description of Parameter
 */
public void layoutContainer(Container target) {
    synchronized (target.getTreeLock()) {
        Insets insets = target.getInsets();
        int maxheight = target.getHeight() - (insets.top + insets.bottom + _vgap * 2);
        int nmembers = target.getComponentCount();
        int y = 0;

        Dimension preferredSize = preferredLayoutSize(target);
        Dimension targetSize = target.getSize();

        switch (_valign) {
        case TOP:
            y = insets.top;
            break;
        case CENTER:
            y = (targetSize.height - preferredSize.height) / 2;
            break;
        case BOTTOM:
            y = targetSize.height - preferredSize.height - insets.bottom;
            break;
        }

        for (int i = 0; i < nmembers; i++) {
            Component m = target.getComponent(i);
            if (m.isVisible()) {
                Dimension d = m.getPreferredSize();
                m.setSize(d.width, d.height);

                if ((y + d.height) <= maxheight) {
                    if (y > 0) {
                        y += _vgap;
                    }

                    int x = 0;
                    switch (_halign) {
                    case LEFT:
                        x = insets.left;
                        break;
                    case CENTER:
                        x = (targetSize.width - d.width) / 2;
                        break;
                    case RIGHT:
                        x = targetSize.width - d.width - insets.right;
                        break;
                    }

                    m.setLocation(x, y);

                    y += d.getHeight();

                } else {
                    break;
                }
            }
        }
    }
}

From source file:GraphPaperLayout.java

/**
 * Algorithm for calculating the largest minimum or preferred cell size.
 * <P>/*from   w w w  .  j av  a  2s. co m*/
 * Largest cell size is calculated by getting the applicable size of each
 * component and keeping the maximum value, dividing the component's width by
 * the number of columns it is specified to occupy and dividing the
 * component's height by the number of rows it is specified to occupy.
 * 
 * @param parent
 *          the container in which to do the layout.
 * @param isPreferred
 *          true for calculating preferred size, false for calculating minimum
 *          size.
 * @return the largest cell size required.
 */
protected Dimension getLargestCellSize(Container parent, boolean isPreferred) {
    int ncomponents = parent.getComponentCount();
    Dimension maxCellSize = new Dimension(0, 0);
    for (int i = 0; i < ncomponents; i++) {
        Component c = parent.getComponent(i);
        Rectangle rect = compTable.get(c);
        if (c != null && rect != null) {
            Dimension componentSize;
            if (isPreferred) {
                componentSize = c.getPreferredSize();
            } else {
                componentSize = c.getMinimumSize();
            }
            // Note: rect dimensions are already asserted to be > 0 when the
            // component is added with constraints
            maxCellSize.width = Math.max(maxCellSize.width, componentSize.width / rect.width);
            maxCellSize.height = Math.max(maxCellSize.height, componentSize.height / rect.height);
        }
    }
    return maxCellSize;
}

From source file:CircleLayoutTest.java

public void layoutContainer(Container parent) {
    setSizes(parent);// w  w  w.j  a v  a2  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

Rectangle getComponentBounds(Component component, boolean doPreferred) {
    XYConstraints constraints = (XYConstraints) info.get(component);
    if (constraints == null)
        constraints = defaultConstraints;
    Rectangle r = new Rectangle(constraints.x, constraints.y, constraints.width, constraints.height);
    if (r.width <= 0 || r.height <= 0) {
        Dimension d = doPreferred ? component.getPreferredSize() : component.getMinimumSize();
        if (r.width <= 0)
            r.width = d.width;/*w ww. ja va2s  .c  om*/
        if (r.height <= 0)
            r.height = d.height;
    }
    return r;
}

From source file:GraphPaperTest.java

/**
 * Algorithm for calculating the largest minimum or preferred cell size.
 * <p>//  w  ww .ja  v a 2 s . c  om
 * Largest cell size is calculated by getting the applicable size of each
 * component and keeping the maximum value, dividing the component's width
 * by the number of columns it is specified to occupy and dividing the
 * component's height by the number of rows it is specified to occupy.
 * 
 * @param parent
 *            the container in which to do the layout.
 * @param isPreferred
 *            true for calculating preferred size, false for calculating
 *            minimum size.
 * @return the largest cell size required.
 */
protected Dimension getLargestCellSize(Container parent, boolean isPreferred) {
    int ncomponents = parent.getComponentCount();
    Dimension maxCellSize = new Dimension(0, 0);
    for (int i = 0; i < ncomponents; i++) {
        Component c = parent.getComponent(i);
        Rectangle rect = (Rectangle) compTable.get(c);
        if (c != null && rect != null) {
            Dimension componentSize;
            if (isPreferred) {
                componentSize = c.getPreferredSize();
            } else {
                componentSize = c.getMinimumSize();
            }
            // Note: rect dimensions are already asserted to be > 0 when the
            // component is added with constraints
            maxCellSize.width = Math.max(maxCellSize.width, componentSize.width / rect.width);
            maxCellSize.height = Math.max(maxCellSize.height, componentSize.height / rect.height);
        }
    }
    return maxCellSize;
}

From source file:TableRenderDemo.java

private void initColumnSizes(JTable table) {
    MyTableModel model = (MyTableModel) table.getModel();
    TableColumn column = null;//from   w  w  w.  j  a v a  2  s.c  o m
    Component comp = null;
    int headerWidth = 0;
    int cellWidth = 0;
    Object[] longValues = model.longValues;
    TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();

    for (int i = 0; i < 5; i++) {
        column = table.getColumnModel().getColumn(i);

        comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
        headerWidth = comp.getPreferredSize().width;

        comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table,
                longValues[i], false, false, 0, i);
        cellWidth = comp.getPreferredSize().width;

        if (DEBUG) {
            System.out.println("Initializing width of column " + i + ". " + "headerWidth = " + headerWidth
                    + "; cellWidth = " + cellWidth);
        }

        // XXX: Before Swing 1.1 Beta 2, use setMinWidth instead.
        column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    }
}

From source file:ColumnLayout.java

/**
 * The method that actually performs the layout. Called by the Container
 *//*from  w w  w  .  j  av a 2s.  c om*/
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:org.jdal.swing.form.SimpleBoxFormBuilder.java

/**
 * Add a component to Form at position pointer by cursor, 
 * Increments cursor by one.//from   w w  w.  j a  va 2s. com
 * @param c Component to add
 */
public void add(Component c) {
    if (debug) {
        if (c instanceof JComponent)
            ((JComponent) c).setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
    }
    addBox(c);

    if (rowHeight < Short.MAX_VALUE) {
        Dimension d = c.getPreferredSize();
        d.height = rowHeight;
        c.setPreferredSize(d);

        c.setMinimumSize(d);

        if (!c.isMaximumSizeSet() || c.getMaximumSize().getHeight() > rowHeight) {
            c.setMaximumSize(new Dimension(Short.MAX_VALUE, rowHeight));
        }
    }

}

From source file:components.TableRenderDemo.java

private void initColumnSizes(JTable table) {
    MyTableModel model = (MyTableModel) table.getModel();
    TableColumn column = null;/*from   w  w w.  java2 s  .  c o  m*/
    Component comp = null;
    int headerWidth = 0;
    int cellWidth = 0;
    Object[] longValues = model.longValues;
    TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();

    for (int i = 0; i < 5; i++) {
        column = table.getColumnModel().getColumn(i);

        comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
        headerWidth = comp.getPreferredSize().width;

        comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table,
                longValues[i], false, false, 0, i);
        cellWidth = comp.getPreferredSize().width;

        if (DEBUG) {
            System.out.println("Initializing width of column " + i + ". " + "headerWidth = " + headerWidth
                    + "; cellWidth = " + cellWidth);
        }

        column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    }
}