Java JTextPane appendToPane(JTextPane textPane, String text, Color foregroundColor, Color backgroundColor)

Here you can find the source of appendToPane(JTextPane textPane, String text, Color foregroundColor, Color backgroundColor)

Description

Append some text in one JTextPane, using colors.

License

Open Source License

Parameter

Parameter Description
textPane Text pane to be appended.
text Text to be appended.
foregroundColor Color of the foreground of the text.
backgroundColor Color of the background of the text.

Declaration

public static void appendToPane(JTextPane textPane, String text, Color foregroundColor, Color backgroundColor) 

Method Source Code

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

import java.awt.Color;

import javax.swing.JTextPane;

import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class Main {
    /**//from w ww . j a  va2  s .co m
     * Append some text in one JTextPane, using colors.
     * 
     * @param textPane Text pane to be appended.
     * @param text Text to be appended.
     * @param foregroundColor Color of the foreground of the text.
     * @param backgroundColor Color of the background of the text.
     */
    public static void appendToPane(JTextPane textPane, String text, Color foregroundColor, Color backgroundColor) {

        StyleContext sc = StyleContext.getDefaultStyleContext();

        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, foregroundColor);
        aset = sc.addAttribute(aset, StyleConstants.Background, backgroundColor);
        //aset = sc.addAttribute( aset, StyleConstants.FontFamily, "Lucida Console" );
        //aset = sc.addAttribute( aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED );

        int len = textPane.getDocument().getLength();
        textPane.setCaretPosition(len);
        textPane.setCharacterAttributes(aset, false);
        textPane.replaceSelection(text);

    }
}

Related

  1. addStyle(JTextPane textPane, String name, Color foreground)
  2. appendAsGreen(JTextPane pane, String fontName, int fontSize, String s)
  3. appendAsMessage(JTextPane pane, String fontName, int fontSize, String s)
  4. appendString(String str, JTextPane jtext)
  5. applyRegex(String regex, JTextPane pane, Color highlightColor)
  6. centerText(JTextPane pane)
  7. createJTextPane(String text, Color backgroundColor)
  8. createTextPaneScrollPane(JTextPane textPane)