import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class JTextPaneTest extends JFrame {
private JTextPane jtp;
static SimpleAttributeSet BLUE = new SimpleAttributeSet();
static SimpleAttributeSet DEFAULT = new SimpleAttributeSet();
static SimpleAttributeSet INSERT = new SimpleAttributeSet();
static {
StyleConstants.setForeground(BLUE, Color.blue);
StyleConstants.setFontFamily(BLUE, "Helvetica");
StyleConstants.setFontSize(BLUE, 12);
StyleConstants.setForeground(DEFAULT, Color.black);
StyleConstants.setFontFamily(DEFAULT, "Helvetica");
StyleConstants.setFontSize(DEFAULT, 12);
StyleConstants.setForeground(INSERT, Color.red);
StyleConstants.setFontFamily(INSERT, "Helvetica");
StyleConstants.setItalic(INSERT,true);
StyleConstants.setBold(INSERT,true);
StyleConstants.setFontSize(INSERT, 12);
}
class WindowListener extends WindowAdapter {
public void windowClosing(WindowEvent event) {
dispose();
System.exit(0);
}
}
public JTextPaneTest() {
super("JTextPane - Test");
jtp = new JTextPane();
getContentPane().add(jtp);
appendText("Dies soll in blau angezeigt werden\n",BLUE);
appendText("Dies soll mit der Standard-Einstellung angezeigt werden\n",DEFAULT);
insertText("Füge das an Stelle 15 ein",INSERT,15);
addWindowListener(new WindowListener());
}
protected void insertText(String text, AttributeSet set,int pos) {
try {
jtp.getDocument().insertString(pos, text, set);
} catch (BadLocationException e) {
System.err.println("Bei dem Versuch einen Text in das JTextPane einzufügen ist eine Bad Location Exception aufgetreten");
System.exit(-1);
}
}
protected void appendText(String text, AttributeSet set) {
insertText(text,set,jtp.getDocument().getLength());
}
public static void main(String[] args) {
JTextPaneTest frame = new JTextPaneTest();
frame.setLocation(100, 100);
frame.setSize(300, 200);
frame.setVisible(true);
}
}