Java Swing Font Set setFont(HTMLDocument doc, Font font, Color fg)

Here you can find the source of setFont(HTMLDocument doc, Font font, Color fg)

Description

Sets the default font for an HTML document (e.g., in a tool tip displaying HTML).

License

BSD License

Parameter

Parameter Description
doc The document to modify.
font The font to use.
fg The default foreground color.

Declaration

public static void setFont(HTMLDocument doc, Font font, Color fg) 

Method Source Code


//package com.java2s;
/*/*from w  w w  .  j  av  a  2 s  .  com*/
 * 08/13/2009
 *
 * TipUtil.java - Utility methods for homemade tool tips.
 *
 * This library is distributed under a modified BSD license.  See the included
 * RSyntaxTextArea.License.txt file for details.
 */

import java.awt.Color;
import java.awt.Font;

import javax.swing.text.html.HTMLDocument;

public class Main {
    /**
     * Sets the default font for an HTML document (e.g., in a tool tip
     * displaying HTML).  This is here because when rendering HTML,
     * {@code setFont()} is not honored.
     *
     * @param doc The document to modify.
     * @param font The font to use.
     * @param fg The default foreground color.
     */
    public static void setFont(HTMLDocument doc, Font font, Color fg) {
        doc.getStyleSheet().addRule("body { font-family: " + font.getFamily() + "; font-size: " + font.getSize()
                + "pt" + "; color: " + getHexString(fg) + "; }");
    }

    /**
     * Returns a hex string for the specified color, suitable for HTML.
     *
     * @param c The color.
     * @return The string representation, in the form "<code>#rrggbb</code>",
     *         or <code>null</code> if <code>c</code> is <code>null</code>.
     */
    private static String getHexString(Color c) {

        if (c == null) {
            return null;
        }

        StringBuilder sb = new StringBuilder("#");
        int r = c.getRed();
        if (r < 16) {
            sb.append('0');
        }
        sb.append(Integer.toHexString(r));
        int g = c.getGreen();
        if (g < 16) {
            sb.append('0');
        }
        sb.append(Integer.toHexString(g));
        int b = c.getBlue();
        if (b < 16) {
            sb.append('0');
        }
        sb.append(Integer.toHexString(b));

        return sb.toString();

    }
}

Related

  1. setDefaultFont(Font font)
  2. setDefaultFont(java.awt.Font font)
  3. setFont(@Nonnull final Font font)
  4. setFont(Container container)
  5. setFont(Font font)
  6. setFont(String fontName)
  7. setFontRecursively(final JComponent component, final Font font, final boolean childsOnly)
  8. setFontScale(float scale)
  9. setHtmlFont(HTMLDocument doc, Font font)