Java Swing Font Set setHtmlFont(HTMLDocument doc, Font font)

Here you can find the source of setHtmlFont(HTMLDocument doc, Font font)

Description

Sets the font used for HTML displays to the specified font.

License

Open Source License

Parameter

Parameter Description
doc the HTML document to update
font the font to use

Exception

Parameter Description
NullPointerException if any parameter is null

Declaration

public static void setHtmlFont(HTMLDocument doc, Font font) 

Method Source Code

//package com.java2s;
/*/*from w ww . j a  v  a2s . c  o m*/
 * #%L
 * The AIBench Plugin Manager Plugin
 * %%
 * Copyright (C) 2006 - 2016 Daniel Glez-Pe?a and Florentino Fdez-Riverola
 * %%
 * This program 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, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program 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 General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */

import java.awt.Font;

import java.io.IOException;
import java.io.StringReader;

import javax.swing.text.html.HTMLDocument;

public class Main {
    private static String STYLESHEET = "body { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0;"
            + " font-family: %s; font-size: %dpt;  }"
            + "a, p, li { margin-top: 0; margin-bottom: 0; margin-left: 0;"
            + " margin-right: 0; font-family: %s; font-size: %dpt;  }";

    /**
     * Sets the font used for HTML displays to the specified font. Components
     * that display HTML do not necessarily honor font properties, since the
     * HTML document can override these values. Calling {@code setHtmlFont}
     * after the data is set will force the HTML display to use the font
     * specified to this method.
     * 
     * @param doc
     *            the HTML document to update
     * @param font
     *            the font to use
     * @throws NullPointerException
     *             if any parameter is {@code null}
     */
    public static void setHtmlFont(HTMLDocument doc, Font font) {
        String stylesheet = String.format(STYLESHEET, font.getName(),
                font.getSize(), font.getName(), font.getSize());

        try {
            doc.getStyleSheet().loadRules(new StringReader(stylesheet),
                    null);
        } catch (IOException e) {
            //this should never happen with our sheet
            throw new IllegalStateException(e);
        }
    }
}

Related

  1. setFont(Font font)
  2. setFont(HTMLDocument doc, Font font, Color fg)
  3. setFont(String fontName)
  4. setFontRecursively(final JComponent component, final Font font, final boolean childsOnly)
  5. setFontScale(float scale)
  6. setItalicFont(JComponent jcomp)
  7. setLabelProperties(JLabel label, Color color, Font font, Color bg)
  8. setMonospacedFont(JComponent component)
  9. setSwingFont(Font font)