Example usage for javax.swing JComponent getFontMetrics

List of usage examples for javax.swing JComponent getFontMetrics

Introduction

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

Prototype

public FontMetrics getFontMetrics(Font font) 

Source Link

Document

Gets the FontMetrics for the specified Font.

Usage

From source file:org.underworldlabs.swing.plaf.base.AcceleratorToolTipUI.java

public Dimension getPreferredSize(JComponent c) {

    Dimension d = super.getPreferredSize(c);

    JToolTip tip = (JToolTip) c;
    String tipText = getTipText(tip) + getAcceleratorStringForRender(tip);

    if (!MiscUtils.isNull(tipText)) {

        Font font = c.getFont();//from   w  w w.j  a v  a 2s  .co m
        FontMetrics fm = c.getFontMetrics(font);
        d.width = fm.stringWidth(tipText) + 15;

    } else {

        d.width += 10;
    }

    return d;
}

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  ww w .j  a va 2 s . 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));
            }
        }
    }
}