Example usage for javax.swing JComponent setMaximumSize

List of usage examples for javax.swing JComponent setMaximumSize

Introduction

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

Prototype

@BeanProperty(description = "The maximum size of the component.")
public void setMaximumSize(Dimension maximumSize) 

Source Link

Document

Sets the maximum size of this component to a constant value.

Usage

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  . j  av  a  2  s  .com
    component.setPreferredSize(dim);
    component.setMaximumSize(dim);
    component.setMinimumSize(dim);
}

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  w  w  w .  ja va 2s .co  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));
            }
        }
    }
}