Java tutorial
//package com.java2s; /* * $Id: SwingXUtilities.java,v 1.11 2009/04/02 20:35:54 kschaefe Exp $ * * Copyright 2008 Sun Microsystems, Inc., 4150 Network Circle, * Santa Clara, California 95054, U.S.A. All rights reserved. * * 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; either * version 2.1 of the License, or (at your option) any later version. * * 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. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ 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); } } }