Java JTextPane appendAsGreen(JTextPane pane, String fontName, int fontSize, String s)

Here you can find the source of appendAsGreen(JTextPane pane, String fontName, int fontSize, String s)

Description

append As Green

License

Open Source License

Declaration

public static void appendAsGreen(JTextPane pane, String fontName, int fontSize, String s) 

Method Source Code


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

import java.awt.Color;

import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;

import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class Main {
    public static final Color SUCCESSFUL_GREEN = new Color(0x3E8927);

    public static void appendAsGreen(JTextPane pane, String fontName, int fontSize, String s) {
        if (pane.getStyle("greenStyle") == null) {
            Style greenStyle = pane.addStyle("greenStyle", null);
            StyleConstants.setForeground(greenStyle, SUCCESSFUL_GREEN);
            StyleConstants.setFontFamily(greenStyle, fontName);
            StyleConstants.setFontSize(greenStyle, fontSize);
        }//from   w  ww.  j a  v a2s .c  o m

        StyledDocument doc = (StyledDocument) pane.getDocument();
        try {
            doc.insertString(doc.getLength(), s, pane.getStyle("greenStyle"));
        } catch (BadLocationException ex) {
        }
    }
}

Related

  1. addStyle(JTextPane textPane, String name, Color foreground)
  2. appendAsMessage(JTextPane pane, String fontName, int fontSize, String s)
  3. appendString(String str, JTextPane jtext)
  4. appendToPane(JTextPane textPane, String text, Color foregroundColor, Color backgroundColor)
  5. applyRegex(String regex, JTextPane pane, Color highlightColor)