Reputation: 1916
I'm creating a basic Swing applcation. I have jTextPane and a button. If the user selects text in the jTextPane and presses the button, it bolds the selected text. This is done by modifying the StyleDocument.
I want to be able to save the styled text temporarily, close the previous jTextPane, open up a new jTextPane, and populate it based off the previous saved styled text. jTextPane takes a StyleDocument in the constructor, but when I try to construct a new jTextPane with the old StyleDocument, it doesn't appear to be working. I figure it's because I'm just passing in the reference from the previous jTextPane StyleDocument which is eventually removed.
Is there any way to copy or clone a StyleDocument? I understand it's an interface, but is it using a DefaultStyledDocument? Is there a better way of doing this?
Upvotes: 2
Views: 1597
Reputation: 57381
Document could be replaced with a new instance e.g. when you set EditorKit or call setText() (setText recreates document in some cases).
So when you store the referece to the old Document call setDocument(oldDoc) after all preparations are done.
Or you can store old content and set it back using something like this http://java-sl.com/editor_kit_tutorial.html
Read about Reader and Writer and use code from the "Example" section.
Upvotes: 3