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

/**
 * A debugging utility that prints to stdout the component's minimum,
 * preferred, and maximum sizes./*from www  .j  ava 2 s  .  co  m*/
 */
public static void printSizes(Component c) {
    System.out.println("minimumSize = " + c.getMinimumSize());
    System.out.println("preferredSize = " + c.getPreferredSize());
    System.out.println("maximumSize = " + c.getMaximumSize());
}

From source file:Main.java

public static void setSizeToPreferredSizeTree(java.awt.Component c) {
    if (c instanceof java.awt.Container) {
        java.awt.Container container = (java.awt.Container) c;
        for (java.awt.Component component : container.getComponents()) {
            setSizeToPreferredSizeTree(component);
        }/*from  ww w .  ja  v  a  2 s . c o m*/
    }
    c.setSize(c.getPreferredSize());
}

From source file:Main.java

static public JDialog addDialogWindow(Frame mainWindow, Component jpanel, String title) {
    JDialog dialog = new JDialog(mainWindow, title, true);
    dialog.getContentPane().setLayout(new BorderLayout());
    dialog.getContentPane().add(jpanel, BorderLayout.CENTER);
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    dialog.pack();//from  w  ww.j ava  2s .c o  m
    dialog.setLocationRelativeTo(mainWindow);
    dialog.setSize(jpanel.getPreferredSize());
    dialog.setVisible(true);
    return dialog;
}

From source file:com.clank.launcher.swing.SwingHelper.java

/**
 * Equalize the width of the given components.
 *
 * @param component component//from www.j  a va 2  s .com
 */
public static void equalWidth(Component... component) {
    double widest = 0;
    for (Component comp : component) {
        Dimension dim = comp.getPreferredSize();
        if (dim.getWidth() > widest) {
            widest = dim.getWidth();
        }
    }

    for (Component comp : component) {
        Dimension dim = comp.getPreferredSize();
        comp.setPreferredSize(new Dimension((int) widest, (int) dim.getHeight()));
    }
}

From source file:Main.java

static public JDialog addModelessWindow(Frame mainWindow, Component jpanel, String title) {
    JDialog dialog = new JDialog(mainWindow, title, true);
    dialog.getContentPane().setLayout(new BorderLayout());
    dialog.getContentPane().add(jpanel, BorderLayout.CENTER);
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    dialog.pack();/* w w w  . ja v a  2 s .  com*/
    dialog.setLocationRelativeTo(mainWindow);
    dialog.setModalityType(ModalityType.MODELESS);
    dialog.setSize(jpanel.getPreferredSize());
    dialog.setVisible(true);
    return dialog;
}

From source file:net.sf.webphotos.util.Util.java

/**
 * PackColumn sets the preferred width of the visible column specified by
 * vColIndex. The column will be just wide enough to show the column head
 * and the widest cell in the column. margin pixels are added to the left
 * and right (resulting in an additional width of 2*margin pixels).
 *
 * @param table The table you want to resize a column.
 * @param vColIndex The column number.//from  www  . j  a  v  a  2 s .c  o m
 * @param margin Extra spaces for each side of column.
 */
public static void packColumn(JTable table, int vColIndex, int margin) {
    DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
    TableColumn col = colModel.getColumn(vColIndex);
    int width;

    // Get width of column header
    javax.swing.table.TableCellRenderer renderer = col.getHeaderRenderer();
    if (renderer == null) {
        renderer = table.getTableHeader().getDefaultRenderer();
    }
    java.awt.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);
}

From source file:Main.java

/**
 * Adjusts the widths and heights of the cells of the supplied table to fit their contents.
 *//*w w  w  . ja va2 s  .  c o  m*/
public static void sizeToContents(JTable table) {
    TableModel model = table.getModel();
    TableColumn column = null;
    Component comp = null;
    int ccount = table.getColumnModel().getColumnCount(), rcount = model.getRowCount(), cellHeight = 0;

    for (int cc = 0; cc < ccount; cc++) {
        int headerWidth = 0, cellWidth = 0;
        column = table.getColumnModel().getColumn(cc);
        try {
            comp = column.getHeaderRenderer().getTableCellRendererComponent(null, column.getHeaderValue(),
                    false, false, 0, 0);
            headerWidth = comp.getPreferredSize().width;
        } catch (NullPointerException e) {
            // getHeaderRenderer() this doesn't work in 1.3
        }

        for (int rr = 0; rr < rcount; rr++) {
            Object cellValue = model.getValueAt(rr, cc);
            comp = table.getDefaultRenderer(model.getColumnClass(cc)).getTableCellRendererComponent(table,
                    cellValue, false, false, 0, cc);
            Dimension psize = comp.getPreferredSize();
            cellWidth = Math.max(psize.width, cellWidth);
            cellHeight = Math.max(psize.height, cellHeight);
        }
        column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    }

    if (cellHeight > 0) {
        table.setRowHeight(cellHeight);
    }
}

From source file:Main.java

public static Dimension getSize(Component c, int sizeflag) {
    if (c == null) {
        return new Dimension(0, 0);
    }/*from  w  w w  .java 2 s .  co m*/
    //special case due to swing bug: html labels need to know the current parent layout's size
    if (c instanceof JLabel) {
        View v = (View) ((JLabel) c).getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
        if (v != null) {
            switch (sizeflag) {
            case MIN: {
                Dimension d = new Dimension(c.getPreferredSize());
                d.width = 1;
                return d;
            }
            case PREF: {
                Dimension d = new Dimension(c.getPreferredSize());
                return d;
            }
            case MAX: {
                Dimension d = new Dimension(10240, 10240);
                d.width = 1;
                return d;
            }
            }
        }
    }
    //
    switch (sizeflag) {
    case MIN: {
        return new Dimension(c.getMinimumSize());
    }
    case PREF: {
        return new Dimension(c.getPreferredSize());
    }
    case MAX: {
        return new Dimension(c.getMaximumSize());
    }
    }
    return new Dimension(c.getPreferredSize());
}

From source file:Main.java

static public JDialog addModelessWindow(Window mainWindow, Component jpanel, String title) {
    JDialog dialog;//from  ww  w  .  j a  v  a 2s . c  om
    if (mainWindow != null) {
        dialog = new JDialog(mainWindow, title);
    } else {
        dialog = new JDialog();
        dialog.setTitle(title);
    }
    dialog.getContentPane().setLayout(new BorderLayout());
    dialog.getContentPane().add(jpanel, BorderLayout.CENTER);
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    dialog.pack();
    dialog.setLocationRelativeTo(mainWindow);
    dialog.setModalityType(ModalityType.MODELESS);
    dialog.setSize(jpanel.getPreferredSize());
    dialog.setVisible(true);
    return dialog;
}

From source file:edu.ku.brc.af.core.NavBox.java

/**
 * Refreshes - meaning it makes sure it is resized (layed out) and drawn.
 * @param nbi the box to refresh/*  ww  w  .  j a va2 s  . c o m*/
 */
public static void refresh(final NavBoxItemIFace nbi) {
    if (nbi != null) {
        Component comp = nbi.getUIComponent();
        comp.invalidate();
        comp.doLayout();
        comp.setSize(comp.getPreferredSize());
        comp.repaint();
        log.debug("comp " + comp.getPreferredSize() + " " + comp.getSize()); //$NON-NLS-1$ //$NON-NLS-2$

        Container parentComp = nbi.getUIComponent().getParent();
        if (parentComp instanceof NavBox) {
            refresh((NavBox) parentComp);
        } else if (parentComp instanceof JScrollPane) {
            // this must be a scrollable NavBox;
            // let's get the actual NavBox
            // container heirarchy is NavBox -> JScrollPane -> NavBoxItem
            parentComp = parentComp.getParent().getParent();
            refresh((NavBox) parentComp);
        }
    }
}