Java Swing Font makeHtmlPane(CharSequence text, Font font)

Here you can find the source of makeHtmlPane(CharSequence text, Font font)

Description

Creates a JEditorPane that displays a message.

License

Open Source License

Parameter

Parameter Description
text the message of the editor pane in HTML format.
font the font to be used in the message.

Return

a JEditorPane that displays a message.

Declaration

public static JEditorPane makeHtmlPane(CharSequence text, Font font) 

Method Source Code

//package com.java2s;
/*/*  ww w .jav  a 2 s  .c om*/
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2008-2010 Sun Microsystems, Inc.
 *      Portions Copyright 2011-2015 ForgeRock AS
 */

import java.awt.Font;

import javax.swing.BorderFactory;

import javax.swing.JComponent;

import javax.swing.JEditorPane;

import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

public class Main {
    /**
     * Creates a JEditorPane that displays a message.
     * @param text the message of the editor pane in HTML format.
     * @param font the font to be used in the message.
     * @return a JEditorPane that displays a message.
     */
    public static JEditorPane makeHtmlPane(CharSequence text, Font font) {
        JEditorPane pane = new JEditorPane();
        pane.setContentType("text/html");
        pane.setFont(font);
        if (text != null) {
            pane.setText(applyFont(text, font));
        }
        pane.setEditable(false);
        pane.setBorder(new EmptyBorder(0, 0, 0, 0));
        pane.setOpaque(false);
        pane.setFocusCycleRoot(false);
        return pane;
    }

    /**
     * Returns a String that contains the html passed as parameter with a span
     * applied.  The span style corresponds to the Font specified as parameter.
     * The goal of this method is to be able to specify a font for an HTML string.
     *
     * @param html the original html text.
     * @param font the font to be used to generate the new HTML.
     * @return a string that represents the original HTML with the font specified
     * as parameter.
     */
    public static String applyFont(CharSequence html, Font font) {
        return "<span style=\"" + getFontStyle(font) + "\">" + html + "</span>";
    }

    /**
     * Sets the border in a given component.  If the component already has a
     * border, creates a compound border.
     * @param comp the component.
     * @param border the border to be set.
     */
    public static void setBorder(JComponent comp, Border border) {
        if (comp.getBorder() != null) {
            comp.setBorder(BorderFactory.createCompoundBorder(comp.getBorder(), border));
        } else {
            comp.setBorder(border);
        }
    }

    /**
     * Returns the HTML style representation for the given font.
     * @param font the font for which we want to get an HTML style representation.
     * @return the HTML style representation for the given font.
     */
    private static String getFontStyle(Font font) {
        StringBuilder buf = new StringBuilder();

        buf.append("font-family:").append(font.getName()).append(";font-size:").append(font.getSize()).append("pt");

        if (font.isItalic()) {
            buf.append(";font-style:italic");
        }

        if (font.isBold()) {
            buf.append(";font-weight:bold;");
        }

        return buf.toString();
    }
}

Related

  1. installColorsAndFont(Component c, Color background, Color foreground, Font font)
  2. installLargerDefaultFonts()
  3. listFonts()
  4. makeFontBold(JComponent c)
  5. makeFontsPlain()
  6. makePlainTextPane(String text, Font font)
  7. mildFont(T comp)
  8. modifyLabelFont(JLabel label, int style, int delta)
  9. newLabel(String text, int fontStyle)