Calculates the dimensions that a given text string requires when rendered as HTML text in a label component. - Java Swing

Java examples for Swing:Swing HTML

Description

Calculates the dimensions that a given text string requires when rendered as HTML text in a label component.

Demo Code

/*/*from   w ww .ja v  a 2s  . c om*/
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2011, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
//package com.java2s;

import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.text.View;

public class Main {
    /**
     * Calculates the dimensions that a given text string requires when rendered
     * as HTML text in a label component.
     * <p>
     * The method used is adapted from that described in a blog post by Morten Nobel:
     * <blockquote>
     * http://blog.nobel-joergensen.com/2009/01/18/changing-preferred-size-of-a-html-jlabel/
     * </blockquote>
     * 
     * @param labelText the text to render, optionally enclosed in {@code <html>...</html>} tags
     * @param fixedDimSize the size of the fixed dimension (either width or height
     * @param width {@code true} if the fixed dimension is width; {@code false} for height
     * 
     * @return the rendered label text extent
     */
    public static Dimension getHtmlLabelTextExtent(final String labelText,
            final int fixedDimSize, final boolean width) {

        final Dimension[] result = new Dimension[1];

        if (SwingUtilities.isEventDispatchThread()) {
            result[0] = doGetHtmlTextExtent(labelText, fixedDimSize, width);
        } else {
            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    @Override
                    public void run() {
                        result[0] = doGetHtmlTextExtent(labelText,
                                fixedDimSize, width);
                    }
                });

            } catch (Exception ex) {
                // Either an InterruptedException or an InvocationTargetException
                // both of which are fatal
                throw new RuntimeException(ex);
            }
        }

        return result[0];
    }

    /**
     * Helper method for {@linkplain #getHtmlLabelTextExtent(java.lang.String, int, boolean)}.
     * This is required because we are creating and invisibly rendering a {@code JLabel} 
     * object in this method, and being virtuous in our Swing usage we should only do that
     * on the event dispatch thread.
     * 
     * @param labelText the text to render, optionally enclosed in {@code <html>...</html>} tags
     * @param fixedDimSize the size of the fixed dimension (either width or height
     * @param width {@code true} if the fixed dimension is width; {@code false} for height
     * 
     * @return the rendered label text extent
     */
    private static Dimension doGetHtmlTextExtent(String labelText,
            int fixedDimSize, boolean width) {
        final JLabel label = new JLabel();
        if (labelText.startsWith("<html>")) {
            label.setText(labelText);
        } else {
            label.setText("<html>" + labelText + "</html>");
        }

        View view = (View) label
                .getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
        view.setSize(width ? fixedDimSize : 0, width ? 0 : fixedDimSize);

        float w = view.getPreferredSpan(View.X_AXIS);
        float h = view.getPreferredSpan(View.Y_AXIS);

        return new java.awt.Dimension((int) Math.ceil(w),
                (int) Math.ceil(h));
    }
}

Related Tutorials