Saving Fonts in JTextPane

45 Views Asked by At

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?

1

There are 1 best solutions below

1
Nowhere Man On

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) {
}