0

As a part of learning Java GUI Programming with javax.swing I intend to save contents of a JTextPane with Font information. Is there a way to do so?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

1

Yes, you can save the contents of JTextPane using HTMLEditorKit or RTFEditorKit.

StyledDocument doc = (StyledDocument)textPane.getDocument();

HTMLEditorKit kit = new HTMLEditorKit();

BufferedOutputStream out;

try {
    out = new BufferedOutputStream(new FileOutputStream(new File("rich.html")));

    kit.write(out, doc, doc.getStartPosition().getOffset(), doc.getLength());

} catch (FileNotFoundException | IOException e) {
} catch (BadLocationException e) {
}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • I would have said to look at the `StyledDocument` if your answer had not beaten me to it by [checks watch] 17 months. Nice one! – Andrew Thompson Oct 06 '21 at 08:27