Here you can find the source of setTextToPane(JTextPane textPane, String text, Color foregroundColor, Color backgroundColor)
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. |
public static void setTextToPane(JTextPane textPane, String text, Color foregroundColor, Color backgroundColor)
//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); } }