Example usage for javax.swing JComponent getPreferredSize

List of usage examples for javax.swing JComponent getPreferredSize

Introduction

In this page you can find the example usage for javax.swing JComponent getPreferredSize.

Prototype

@Transient
public Dimension getPreferredSize() 

Source Link

Document

If the preferredSize has been set to a non-null value just returns it.

Usage

From source file:com.t3.client.ui.T3Frame.java

private void showGlassPane(JComponent component, int x, int y, boolean modal) {
    component.setSize(component.getPreferredSize());
    component.setLocation(x, y);/*from ww w .j a v a2 s  .c  om*/
    glassPane.setLayout(null);
    glassPane.add(component);
    glassPane.setModel(modal);
    glassPane.setVisible(true);
}

From source file:com.osparking.osparking.Settings_System.java

private void changeComponentHeight(JComponent compo, int height) {
    setComponentSize(compo, new Dimension(compo.getPreferredSize().width, height));
}

From source file:org.bitbucket.mlopatkin.android.logviewer.widgets.UiHelper.java

public static void setWidths(JComponent component, int width) {
    Dimension dim = component.getPreferredSize();
    dim.width = width;/*from  w  w w.jav  a2s.  c  om*/
    component.setPreferredSize(dim);
    component.setMaximumSize(dim);
    component.setMinimumSize(dim);
}

From source file:org.nuclos.client.ui.resplan.header.JHeaderGrid.java

private void calculateGroupingExtents() {
    final Orientation groupOrient = orientation.opposite();
    int preferredGroupingsExtent = 0;
    for (CategoryView categoryView : groupings) {
        int preferredCategorySize = 0;
        for (HeaderCell cell : categoryView.cells) {
            JComponent renderer = setupCellRenderer(cell.levelValue, false);
            Dimension preferredSize = renderer.getPreferredSize();
            int preferredExtent = groupOrient.extentFrom(preferredSize);
            if (renderer instanceof JLabel) {
                // This is a some kind of hack for determing the preferred size of an HTML JLabel
                // using a given width or height. See also:
                // http://blog.nobel-joergensen.com/2009/01/18/changing-preferred-size-of-a-html-jlabel/
                // NOTE: This seems to work for horizontal orientations (i.e. calculating the height for a
                // given width) while for vertical orientations often the dummy value 0 is returned.
                View view = (View) renderer.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
                if (view != null) {
                    float w = (orientation == Orientation.HORIZONTAL) ? cellExtent : 0;
                    float h = (orientation == Orientation.VERTICAL) ? cellExtent : 0;
                    view.setSize(w, h);/*from w w w .j ava  2s  . c om*/
                    float span = (int) Math.ceil(view.getPreferredSpan(groupOrient.swingConstant()));
                    preferredExtent = Math.max(preferredExtent, (int) Math.ceil(span));
                }
            }
            preferredCategorySize = Math.max(preferredCategorySize, preferredExtent + 2);
        }
        categoryView.preferredSize = preferredCategorySize;
        categoryView.size = preferredCategorySize;
        preferredGroupingsExtent += preferredCategorySize + GRID_SIZE;
    }

    // Real size and preferred size may vary
    int realGroupingsExtent = groupOrient.extentFrom(getSize());
    int delta = realGroupingsExtent - preferredGroupingsExtent;
    if (delta > 0) {
        int gap = delta / groupings.length;
        for (CategoryView v : groupings) {
            v.size += gap;
        }
        int rem = delta % groupings.length;
        if (rem > 0 && groupings.length > 0) {
            groupings[groupings.length - 1].size += rem;
        }
    }
}

From source file:org.nuclos.client.ui.resplan.header.JHeaderGrid.java

public int getPreferredCellExtent() {
    int preferredCellExtent = -1;
    for (CategoryView categoryView : groupings) {
        for (HeaderCell cell : categoryView.cells) {
            JComponent renderer = setupCellRenderer(cell.levelValue, false);
            Dimension preferredSize = renderer.getPreferredSize();
            preferredCellExtent = Math.max(preferredCellExtent, orientation.extentFrom(preferredSize) + 2);
        }/*from w w  w . java 2  s.c o m*/
    }
    if (preferredCellExtent == -1) {
        preferredCellExtent = orientation.extentFrom(DEFAULT_CELL_EXTENT);
    }
    return preferredCellExtent;
}

From source file:org.openmicroscopy.shoola.agents.imviewer.view.ImViewerUI.java

/** 
 * Overridden to the set the location of the {@link ImViewer}.
 * @see TopWindow#setOnScreen() // ww  w .  ja  va2 s  .  c  o m
 */
public void setOnScreen() {
    if (model != null) {
        Browser browser = model.getBrowser();
        Rectangle r = null;
        if (browser != null) {
            JComponent comp = browser.getUI();
            Dimension size = comp.getPreferredSize();
            int w = size.width;
            int h = size.height;
            ViewerPreferences pref = ImViewerFactory.getPreferences();
            if (pref != null) {
                r = pref.getViewerBounds();
                w = r.width;
                h = r.height;
                if (w <= 0)
                    w = size.width;
                if (h <= 0)
                    h = size.height;
            }
            if (pref != null) {
                setBounds(r.x, r.y, w, h);
                setVisible(true);
            } else
                packWindow();
        } else
            packWindow();
        UIUtilities.incrementRelativeToAndShow(model.getRequesterBounds(), this);
    } else {
        packWindow();
        UIUtilities.incrementRelativeToAndShow(null, this);
    }
}

From source file:org.ut.biolab.medsavant.client.view.component.KeyValuePairPanel.java

public void ellipsifyValues(int width) {
    int maxKeyWidth = 0;
    int[] maxAdditionalColumnsWidth = new int[additionalColumns];

    for (JComponent keyComp : keyKeyComponentMap.values()) {
        maxKeyWidth = Math.max(maxKeyWidth, keyComp.getPreferredSize().width);
    }//from  www  .j a  v a 2  s . c  o  m

    for (String k : keyValueComponentMap.keySet()) {
        for (int i = 0; i < additionalColumns; i++) {
            JComponent extraComp = getAdditionalColumn(k, i);
            if (extraComp != null) {
                maxAdditionalColumnsWidth[i] = Math.max(maxAdditionalColumnsWidth[i],
                        extraComp.getPreferredSize().width);
            }
        }
    }

    width -= maxKeyWidth;
    for (int i : maxAdditionalColumnsWidth) {
        width -= i;
    }

    for (String k : keyValueComponentMap.keySet()) {
        JComponent comp = getComponent(k);
        if (comp != null) {
            int avail = width;
            /*for (int i = 0; i < additionalColumns; i++) {
             JComponent extraComp = getAdditionalColumn(k, i);
             if (extraComp != null) {
             avail -= extraComp.getPreferredSize().width;
             }
             }*/

            if (comp instanceof JLabel) {

                while (avail < comp.getPreferredSize().width) {
                    String text = ((JLabel) comp).getText();
                    if (text.endsWith("")) {
                        if (text.length() > 2) {
                            // Already truncated.
                            text = text.substring(0, text.length() - 2);
                        } else {
                            break; // As short as we can get.  Can't truncate any more.
                        }
                    } else {
                        // Reasonable first truncation is to trim off the last word.
                        int spacePos = text.lastIndexOf(' ');
                        if (spacePos > 0) {
                            text = text.substring(0, spacePos);
                        } else {
                            FontMetrics fm = comp.getFontMetrics(comp.getFont());

                            while (fm.stringWidth(text + "") > avail) {
                                //causes StringIndexOutOfBoundsException if text is empty.  
                                if (text == null || text.length() < 2) {
                                    LOG.info("Text is null or empty in KeyValuePairPanel");
                                    break;
                                }
                                text = text.substring(0, text.length() - 2);
                            }
                            //text = text + "";

                            //text = text.substring(0, text.length() - 1);
                        }
                    }
                    ((JLabel) comp).setText(text + "");
                }
            } else {
                // Can't truncate, but we can force the preferred size.
                comp.setMaximumSize(new Dimension(avail, comp.getPreferredSize().height));
            }
        }
    }
}