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

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

Description

Set the text pane text.

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 setTextToPane(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 {
    /**/* w w w  .j  ava 2  s .com*/
     * Set the text pane text.
     * 
     * @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 setTextToPane(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);
        textPane.setCharacterAttributes(aset, false);

        textPane.setText(text);

    }
}

Related

  1. setSelectionFontSize(JTextPane textPane, int fontSize)
  2. setSelectionForeground(JTextPane textPane, Color color)
  3. setStyle(JTextPane textPane, int start, int length, String name)
  4. setStyle(JTextPane textPane, Style style)
  5. setTabs(int charactersPerTab, JTextPane textpane)
  6. writeImage(final JTextPane jtPane, final ImageIcon msg)