Kleines Beispiel: [HIGHLIGHT="Java"]public static void main(String[] args) throws BadLocationException {
final StyledDocument doc = new DefaultStyledDocument();
final Style styleBold = doc.addStyle("bold", null);
StyleConstants.setBold(styleBold, true);
final Style styleItalic = doc.addStyle("italic", null);
StyleConstants.setItalic(styleItalic, true);
doc.insertString(0, "This is unformatted text\n", null);
doc.insertString(doc.getLength(), "This is ", null);
doc.insertString(doc.getLength(), "formatted", styleBold);
doc.insertString(doc.getLength(), " ", null);
doc.insertString(doc.getLength(), "text", styleItalic);
final JTextPane textPane = new JTextPane(doc);
final JFrame f = new JFrame("Styled Document Fun");
f.setContentPane(new JScrollPane(textPane));
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}[/HIGHLIGHT]
Sieht dann so aus:
This is unformatted text
This is formatted text
Ebenius