简体   繁体   中英

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

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.

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

For the simplest case (eg 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM