Java JTextPane setJTextPaneFont(JTextPane jtp, Font font, Color c)

Here you can find the source of setJTextPaneFont(JTextPane jtp, Font font, Color c)

Description

Utility method for setting the font and color of a JTextPane.

License

Open Source License

Declaration

public static void setJTextPaneFont(JTextPane jtp, Font font, Color c) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;

import java.awt.Font;

import javax.swing.JTextPane;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class Main {
    /**//w ww.j a v  a 2 s  .c om
     * Utility method for setting the font and color of a JTextPane. The
     * result is roughly equivalent to calling setFont(...) and 
     * setForeground(...) on an AWT TextArea.
     */
    public static void setJTextPaneFont(JTextPane jtp, Font font, Color c) {
        // Start with the current input attributes for the JTextPane. This
        // should ensure that we do not wipe out any existing attributes
        // (such as alignment or other paragraph attributes) currently
        // set on the text area.
        MutableAttributeSet attrs = jtp.getInputAttributes();

        // Set the font family, size, and style, based on properties of
        // the Font object. Note that JTextPane supports a number of
        // character attributes beyond those supported by the Font class.
        // For example, underline, strike-through, super- and sub-script.
        StyleConstants.setFontFamily(attrs, font.getFamily());
        StyleConstants.setFontSize(attrs, font.getSize());
        StyleConstants.setItalic(attrs, (font.getStyle() & Font.ITALIC) != 0);
        StyleConstants.setBold(attrs, (font.getStyle() & Font.BOLD) != 0);

        // Set the font color
        StyleConstants.setForeground(attrs, c);

        // Retrieve the pane's document object
        StyledDocument doc = jtp.getStyledDocument();

        // Replace the style for the entire document. We exceed the length
        // of the document by 1 so that text entered at the end of the
        // document uses the attributes.
        doc.setCharacterAttributes(0, doc.getLength() + 1, attrs, false);
    }
}

Related

  1. isSelectionBold(JTextPane textPane)
  2. overrideStyle(String styleName, JTextPane textPane, Style newStyle)
  3. saveFile(JTextPane txt)
  4. scrollTo(JTextPane textPane, int position)
  5. setDefaultTextPaneProperties(JTextPane textPane)
  6. setSelectionFontSize(JTextPane textPane, int fontSize)
  7. setSelectionForeground(JTextPane textPane, Color color)
  8. setStyle(JTextPane textPane, int start, int length, String name)
  9. setStyle(JTextPane textPane, Style style)