Java JEditorPane setDefaultFontToEditorPane(JEditorPane editor, boolean justify)

Here you can find the source of setDefaultFontToEditorPane(JEditorPane editor, boolean justify)

Description

Set the label font to the editor.

License

Open Source License

Parameter

Parameter Description
editor The editor. The CSS stylesheet will be added to it.
justify A flag representing full justification. If true, the text will be fully justified.

Declaration

public static void setDefaultFontToEditorPane(JEditorPane editor, boolean justify) 

Method Source Code

//package com.java2s;
/**/*  w w  w.  j  a va2s .c  o  m*/
 * \cond LICENSE
 * ********************************************************************
 * This is a conditional block for preventing the DoxyGen documentation
 * tool to include this license header within the description of each
 * source code file. If you want to include this block, please define
 * the LICENSE parameter into the provided DoxyFile.
 * ********************************************************************
 *
 * TeXPrinter - A TeX.SX question printer Copyright (c) 2012, Paulo Roberto
 * Massa Cereda All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the project's author nor the names of its contributors
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * ********************************************************************
 * End of the LICENSE conditional block
 * ********************************************************************
 * \endcond
 *
 * UIUtils.java: This class provides UI helper methods.
 * Last revision: paulo at temperantia 26 Feb 2012 05:12
 */

import java.awt.Font;

import javax.swing.*;
import javax.swing.text.html.HTMLDocument;

public class Main {
    /**
     * Set the label font to the editor. When the content type
     * is set to 'text/html', the plain visualization is very ugly, so this
     * method will set the default JLabel font to a JEditorPane.
     *
     * @param editor The editor. The CSS stylesheet will be added to it.
     * @param justify A flag representing full justification. If true, the
     * text will be fully justified.
     */
    public static void setDefaultFontToEditorPane(JEditorPane editor, boolean justify) {

        // get the system font
        Font font = UIManager.getFont("Label.font");

        // set the body rule
        String bodyRule = "body { font-family: " + font.getFamily() + "; " + "font-size: " + font.getSize()
                + "pt; margin-left: 0px; margin-top: 0px; " + (justify ? "text-align: justify;" : "") + " }";

        // set the list rule
        String listRule = "ol { margin-left: 20px; list-style-type: square; }";

        // add the body rule
        ((HTMLDocument) editor.getDocument()).getStyleSheet().addRule(bodyRule);

        // add the list rule
        ((HTMLDocument) editor.getDocument()).getStyleSheet().addRule(listRule);
    }
}

Related

  1. initHtmlComponent(JEditorPane editorPane)
  2. installTextStyle(JEditorPane control, String styleName)
  3. panelForHtml(JEditorPane editorPane)
  4. scrollToTop(final JEditorPane editor)
  5. scrollToTop(JEditorPane editor)
  6. SetEditorProperties(JEditorPane editor, String text)
  7. setJEditorPaneBackground(javax.swing.JEditorPane jEditorPane, Color color)
  8. setParagraphAttributes(final AttributeSet attr, final boolean replace, final JEditorPane editorPane, final StyledDocument doc)
  9. start(JEditorPane textPane)