How to get the content with all attributes of a JTextPane?

145 views Asked by At

I have a JTextPane and on a ActionEvent, I want to save the content of the JTextPane (with the colors, text and other attributes) in another class (as a variable). Later on I want to exchange the content of the JTextPane with the one of the variable.

I have tried to use:

(StyledDocument)myTextPane.getDocument()

and

myTextPane.getStyledDocument()

but both didn't work.

I thought about just give the whole JTextPane over, but apperently the pane gets still updated in the other class...

EDIT: I would only use the content to show it (later) in the JTextPane again. I won't be saving it in a file or something similar.

1

There are 1 answers

4
StanislavL On

Actually it depends on EditorKit you use. Each kit has own format to store/load content.

For the simplest case (e.g. if you use HTMLEditorKit) you can use getText()/setText().

If you need own format and would like to store the content yourself read this

Also you can try to use the AdvancedRTFEditorKit to store the content as RTF (default RTFEditorKit is limited)