Here you can find the source of appendToPane(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 appendToPane(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 { /**//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); } }