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:LCBLayout.java

/**
 * Returns the preferred size using this layout manager.
 *
 * @param parent  the parent.//from  www.  ja  v a 2  s.  c  o  m
 *
 * @return the preferred size using this layout manager.
*/
public Dimension preferredLayoutSize(final Container parent) {

    synchronized (parent.getTreeLock()) {
        final Insets insets = parent.getInsets();
        final int ncomponents = parent.getComponentCount();
        final int nrows = ncomponents / COLUMNS;
        for (int c = 0; c < COLUMNS; c++) {
            for (int r = 0; r < nrows; r++) {
                final Component component = parent.getComponent(r * COLUMNS + c);
                final Dimension d = component.getPreferredSize();
                if (this.colWidth[c] < d.width) {
                    this.colWidth[c] = d.width;
                }
                if (this.rowHeight[r] < d.height) {
                    this.rowHeight[r] = d.height;
                }
            }
        }
        int totalHeight = this.vGap * (nrows - 1);
        for (int r = 0; r < nrows; r++) {
            totalHeight = totalHeight + this.rowHeight[r];
        }
        final int totalWidth = this.colWidth[0] + this.labelGap + this.colWidth[1] + this.buttonGap
                + this.colWidth[2];
        return new Dimension(insets.left + insets.right + totalWidth + this.labelGap + this.buttonGap,
                insets.top + insets.bottom + totalHeight + this.vGap);
    }

}

From source file:EdgeLayoutExample.java

public Dimension preferredLayoutSize(Container parent) {
    synchronized (parent.getTreeLock()) {
        int width = 0;
        int height = 0;

        //Add the preferred widths of all EAST/WEST components
        //Add the preferred height of all NORTH/SOUTH components
        for (int i = 0; i < this.components.size(); i++) {
            Component c = (Component) this.components.get(i);
            if (this.constraints.get(c).equals(WEST) || this.constraints.get(c).equals(EAST)) {
                width += c.getPreferredSize().getWidth();
            } else {
                height += c.getPreferredSize().getHeight();
            }// ww w.  j a  v a 2 s . c o  m
        }

        width += parent.getInsets().right + parent.getInsets().left;
        height += parent.getInsets().top + parent.getInsets().bottom;

        return new Dimension(width, height);
    }
}

From source file:Visuals.RingChart.java

public JPanel addDefenceCharts() {
    lowValue = "Low (" + low + ")";
    ChartPanel ringPanel = drawRingChart();
    ringPanel.setDomainZoomable(true);//from w  w w  . ja v  a2  s . c om
    JPanel thisRingPanel = new JPanel();
    thisRingPanel.setLayout(new BorderLayout());
    String[][] finalRisks = new String[riskCount][4];
    for (int i = 0; i < riskCount; i++) {
        finalRisks[i][0] = risks[i][0];
        finalRisks[i][1] = risks[i][1];
        finalRisks[i][2] = risks[i][2];
        finalRisks[i][3] = risks[i][3];
    }

    JTable table = new JTable(finalRisks, networkDefenceColumns);
    TableColumn tcol = table.getColumnModel().getColumn(2);
    table.removeColumn(tcol);
    table.setEnabled(false);

    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    int width = 0;
    for (int row = 0; row < table.getRowCount(); row++) {
        TableCellRenderer renderer = table.getCellRenderer(row, 1);
        Component comp = table.prepareRenderer(renderer, row, 1);
        width = Math.max(comp.getPreferredSize().width, width);
    }

    table.getColumnModel().getColumn(1).setPreferredWidth(width);

    width = 0;
    for (int row = 0; row < table.getRowCount(); row++) {
        TableCellRenderer renderer = table.getCellRenderer(row, 2);
        Component comp = table.prepareRenderer(renderer, row, 2);
        width = Math.max(comp.getPreferredSize().width, width);
    }

    table.getColumnModel().getColumn(2).setPreferredWidth(width);
    table.setShowHorizontalLines(true);
    table.setRowHeight(40);

    JScrollPane tableScrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    Dimension d = table.getPreferredSize();
    tableScrollPane
            .setPreferredSize(new Dimension((d.width - 400), (table.getRowHeight() + 1) * (riskCount + 1)));

    JLabel right = new JLabel(
            "                                                                                                    ");
    thisRingPanel.add(right, BorderLayout.EAST);
    thisRingPanel.add(ringPanel, BorderLayout.CENTER);
    thisRingPanel.add(tableScrollPane, BorderLayout.SOUTH);
    return thisRingPanel;
}

From source file:RadialLayout.java

/**
 * This is called when the panel is first displayed, and every time its size
 * changes.//from  w  w w  .  ja v a  2 s . c om
 * Note: You CAN'T assume preferredLayoutSize or minimumLayoutSize will be
 * called -- in the case of applets, at least, they probably won't be.
 *
 * @param  parent  the parent.
 * @see LayoutManager
 */
public void layoutContainer(final Container parent) {
    final Insets insets = parent.getInsets();
    final int maxWidth = parent.getSize().width - (insets.left + insets.right);
    final int maxHeight = parent.getSize().height - (insets.top + insets.bottom);
    final int nComps = parent.getComponentCount();
    int x = 0;
    int y = 0;

    // Go through the components' sizes, if neither preferredLayoutSize nor
    // minimumLayoutSize has been called.
    if (this.sizeUnknown) {
        setSizes(parent);
    }

    if (nComps < 2) {
        final Component c = parent.getComponent(0);
        if (c.isVisible()) {
            final Dimension d = c.getPreferredSize();
            c.setBounds(x, y, d.width, d.height);
        }
    } else {
        double radialCurrent = Math.toRadians(90);
        final double radialIncrement = 2 * Math.PI / nComps;
        final int midX = maxWidth / 2;
        final int midY = maxHeight / 2;
        final int a = midX - this.maxCompWidth;
        final int b = midY - this.maxCompHeight;
        for (int i = 0; i < nComps; i++) {
            final Component c = parent.getComponent(i);
            if (c.isVisible()) {
                final Dimension d = c.getPreferredSize();
                x = (int) (midX - (a * Math.cos(radialCurrent)) - (d.getWidth() / 2) + insets.left);
                y = (int) (midY - (b * Math.sin(radialCurrent)) - (d.getHeight() / 2) + insets.top);

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

From source file:LCBLayout.java

/**
 * Lays out the components.//from   ww w  .  j av a 2  s  .  c  o m
 *
 * @param parent  the parent.
 */
public void layoutContainer(final Container parent) {

    synchronized (parent.getTreeLock()) {
        final Insets insets = parent.getInsets();
        final int ncomponents = parent.getComponentCount();
        final int nrows = ncomponents / COLUMNS;
        for (int c = 0; c < COLUMNS; c++) {
            for (int r = 0; r < nrows; r++) {
                final Component component = parent.getComponent(r * COLUMNS + c);
                final Dimension d = component.getPreferredSize();
                if (this.colWidth[c] < d.width) {
                    this.colWidth[c] = d.width;
                }
                if (this.rowHeight[r] < d.height) {
                    this.rowHeight[r] = d.height;
                }
            }
        }
        int totalHeight = this.vGap * (nrows - 1);
        for (int r = 0; r < nrows; r++) {
            totalHeight = totalHeight + this.rowHeight[r];
        }
        final int totalWidth = this.colWidth[0] + this.colWidth[1] + this.colWidth[2];

        // adjust the width of the second column to use up all of parent
        final int available = parent.getWidth() - insets.left - insets.right - this.labelGap - this.buttonGap;
        this.colWidth[1] = this.colWidth[1] + (available - totalWidth);

        // *** DO THE LAYOUT ***
        int x = insets.left;
        for (int c = 0; c < COLUMNS; c++) {
            int y = insets.top;
            for (int r = 0; r < nrows; r++) {
                final int i = r * COLUMNS + c;
                if (i < ncomponents) {
                    final Component component = parent.getComponent(i);
                    final Dimension d = component.getPreferredSize();
                    final int h = d.height;
                    final int adjust = (this.rowHeight[r] - h) / 2;
                    parent.getComponent(i).setBounds(x, y + adjust, this.colWidth[c], h);
                }
                y = y + this.rowHeight[r] + this.vGap;
            }
            x = x + this.colWidth[c];
            if (c == 0) {
                x = x + this.labelGap;
            }
            if (c == 1) {
                x = x + this.buttonGap;
            }
        }

    }

}

From source file:GeMSE.GS.Analysis.Stats.OneSampleCovariancePanel.java

public void ResizeColumnWidth(JTable table) {
    final TableColumnModel columnModel = table.getColumnModel();
    for (int column = 0; column < table.getColumnCount(); column++) {
        int width = 50; // Min width

        TableColumn tableColumn = columnModel.getColumn(column);
        TableCellRenderer renderer = tableColumn.getHeaderRenderer();
        if (renderer == null)
            renderer = table.getTableHeader().getDefaultRenderer();

        Component component = renderer.getTableCellRendererComponent(table, tableColumn.getHeaderValue(), false,
                false, -1, column);/*ww  w  .j  av  a  2  s.c  om*/
        width = Math.max(component.getPreferredSize().width + 5, width);

        for (int row = 0; row < table.getRowCount(); row++) {
            renderer = table.getCellRenderer(row, column);
            Component comp = table.prepareRenderer(renderer, row, column);
            width = Math.max(comp.getPreferredSize().width + 5, width);
        }
        if (width > 400)
            width = 400;
        columnModel.getColumn(column).setPreferredWidth(width);
    }
}

From source file:CenterLayout.java

public void layoutContainer(Container container) {
    int count = container.getComponentCount();
    if (count > 0) {
        Component child = container.getComponent(0);
        java.awt.Insets insets = container.getInsets();
        int availWidth = container.getWidth() - insets.left - insets.right;
        int availHeight = container.getHeight() - insets.top - insets.bottom;
        Dimension preferredSize = child.getPreferredSize();
        double preferredWidth = preferredSize.getWidth();
        double preferredHeight = preferredSize.getHeight();
        int width;
        int height;
        int x;/*from w w w .j av  a  2 s .  com*/
        int y;
        if (preferredWidth < availWidth) {
            x = (int) Math.round(insets.left + (availWidth - preferredWidth) / 2);
            width = (int) Math.round(preferredWidth);
        } else {
            x = insets.left;
            width = availWidth;
        }
        if (preferredHeight < availHeight) {
            y = (int) Math.round(insets.top + (availHeight - preferredHeight) / 2);
            height = (int) Math.round(preferredHeight);
        } else {
            y = insets.top;
            height = availHeight;
        }
        child.setBounds(x, y, width, height);
    }
}

From source file:Visuals.RingChart.java

public JPanel addAVCharts() {
    JPanel thisPanel = new JPanel();
    thisPanel.setLayout(new BorderLayout());
    int tempCriticalCount = 0, tempUpdatedCount = 0;
    String[][] criticalList = new String[critical][4];
    String[][] updatedList = new String[low][4];
    for (int i = 0; i < riskCount; i++) {
        if (risks[i][2].equals("Critical")) {
            criticalList[tempCriticalCount][0] = risks[i][0];
            criticalList[tempCriticalCount][1] = risks[i][1];
            criticalList[tempCriticalCount][3] = risks[i][3];
            tempCriticalCount++;/*from   w  w w.  j a va  2  s  .c o  m*/
        }
        if (risks[i][2].equals("Low")) {
            updatedList[tempUpdatedCount][0] = risks[i][0];
            updatedList[tempUpdatedCount][1] = risks[i][1];
            tempUpdatedCount++;
        }
    }

    JTable criticalTable = new JTable(criticalList, criticalColumns);
    JTable updatedTable = new JTable(updatedList, updatedColumns);
    TableColumn tcol = criticalTable.getColumnModel().getColumn(2);
    criticalTable.removeColumn(tcol);
    TableColumn tcol2 = updatedTable.getColumnModel().getColumn(2);
    updatedTable.removeColumn(tcol2);

    criticalTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    updatedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    criticalTable.setEnabled(false);
    updatedTable.setEnabled(false);

    int width = 0;
    for (int i = 0; i < 3; i++) {
        for (int row = 0; row < criticalTable.getRowCount(); row++) {
            TableCellRenderer renderer = criticalTable.getCellRenderer(row, i);
            Component comp = criticalTable.prepareRenderer(renderer, row, i);
            width = Math.max(comp.getPreferredSize().width, width);
        }
        criticalTable.getColumnModel().getColumn(i).setPreferredWidth(width);
        width = 0;
    }

    for (int i = 0; i < 2; i++) {
        for (int row = 0; row < updatedTable.getRowCount(); row++) {
            TableCellRenderer renderer = updatedTable.getCellRenderer(row, i);
            Component comp = updatedTable.prepareRenderer(renderer, row, i);
            width = Math.max(comp.getPreferredSize().width, width);
        }
        updatedTable.getColumnModel().getColumn(i).setPreferredWidth(width);
        width = 0;
    }

    criticalTable.setShowHorizontalLines(true);
    criticalTable.setRowHeight(40);
    updatedTable.setShowHorizontalLines(true);
    updatedTable.setRowHeight(40);

    JScrollPane criticalTableScrollPane = new JScrollPane(criticalTable,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    JScrollPane updatedTableScrollPane = new JScrollPane(updatedTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    Dimension d = criticalTable.getPreferredSize();
    criticalTableScrollPane.setPreferredSize(
            new Dimension((d.width - 400), (criticalTable.getRowHeight() + 1) * (tempCriticalCount + 1)));

    Dimension d2 = updatedTable.getPreferredSize();
    updatedTableScrollPane.setPreferredSize(
            new Dimension((d.width - 400), (updatedTable.getRowHeight() + 1) * (tempUpdatedCount + 1)));

    Font titleFonts = new Font("Calibri", Font.BOLD, 30);

    JLabel criticalLabel = new JLabel(" Critical    ");
    criticalLabel.setFont(titleFonts);
    criticalLabel.setForeground(new Color(230, 27, 27));
    JPanel leftPanel = new JPanel();
    leftPanel.setLayout(new BorderLayout());
    leftPanel.add(criticalLabel, BorderLayout.WEST);
    leftPanel.add(criticalTableScrollPane, BorderLayout.CENTER);

    JLabel updatedLabel = new JLabel(" Updated ");
    updatedLabel.setFont(titleFonts);
    updatedLabel.setForeground(new Color(47, 196, 6));
    JPanel rightPanel = new JPanel();
    rightPanel.setLayout(new BorderLayout());
    rightPanel.add(updatedLabel, BorderLayout.WEST);
    rightPanel.add(updatedTableScrollPane, BorderLayout.CENTER);

    if (tempCriticalCount != 0)
        thisPanel.add(leftPanel, BorderLayout.NORTH);
    if (tempUpdatedCount != 0)
        thisPanel.add(rightPanel, BorderLayout.SOUTH);
    return thisPanel;
}

From source file:VerticalFlowLayout.java

/**
 *  Description of the Method/* ww w  .ja va  2 s .  com*/
 *
 *@param  target  Description of Parameter
 *@return         Description of the Returned Value
 */
public Dimension preferredLayoutSize(Container target) {
    synchronized (target.getTreeLock()) {
        Dimension dim = new Dimension(0, 0);
        int nmembers = target.getComponentCount();
        boolean firstVisibleComponent = true;

        for (int ii = 0; ii < nmembers; ii++) {
            Component m = target.getComponent(ii);
            if (m.isVisible()) {
                Dimension d = m.getPreferredSize();
                dim.width = Math.max(dim.width, d.width);
                if (firstVisibleComponent) {
                    firstVisibleComponent = false;
                } else {
                    dim.height += _vgap;
                }
                dim.height += d.height;
            }
        }
        Insets insets = target.getInsets();
        dim.width += insets.left + insets.right + _hgap * 2;
        dim.height += insets.top + insets.bottom + _vgap * 2;
        return dim;
    }
}

From source file:VerticalFlowLayout.java

/**
 *  Description of the Method//from w ww.j ava 2 s . com
 *
 *@param  target  Description of Parameter
 *@return         Description of the Returned Value
 */
public Dimension minimumLayoutSize(Container target) {
    synchronized (target.getTreeLock()) {
        Dimension dim = new Dimension(0, 0);
        int nmembers = target.getComponentCount();
        boolean firstVisibleComponent = true;

        for (int ii = 0; ii < nmembers; ii++) {
            Component m = target.getComponent(ii);
            if (m.isVisible()) {
                Dimension d = m.getPreferredSize();
                dim.width = Math.max(dim.width, d.width);
                if (firstVisibleComponent) {
                    firstVisibleComponent = false;
                } else {
                    dim.height += _vgap;
                }
                dim.height += d.height;
            }
        }
        Insets insets = target.getInsets();
        dim.width += insets.left + insets.right + _hgap * 2;
        dim.height += insets.top + insets.bottom + _vgap * 2;
        return dim;
    }
}