Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to save a "Styled" document

 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have created a small application for editing text using StyledDocument and JTextPane. Now, you can set the font size, font colour, and other font related stuff. My question is, how could I save this document? What is the format? When I reopen this document, all the decorations I have done to the file should be there. How do I do this? Please help.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I did this several years ago, I saved it as XML. The model for a styled document does have a tree structure based on javax.swing.text.Element, so it isn't terribly hard to convert that tree structure into an equivalent XML tree structure. I start by getting the root of the model's tree:



and then I recursively convert that to an XML document. Elements in the tree become elements in the XML document, and attributes of the Elements become attributes of the XML elements, and likewise leaf nodes of the tree correspond to text nodes of the XML document. Of course there's a similar process to take the XML and rebuild the model of a styled document from it.

The process isn't very complicated but it does take some fiddling around with the details of javax.swing.text.Element, plus observing what you actually get out of a styled document.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:When I did this several years ago, I saved it as XML. The model for a styled document does have a tree structure based on javax.swing.text.Element, so it isn't terribly hard to convert that tree structure into an equivalent XML tree structure. I start by getting the root of the model's tree:



and then I recursively convert that to an XML document. Elements in the tree become elements in the XML document, and attributes of the Elements become attributes of the XML elements, and likewise leaf nodes of the tree correspond to text nodes of the XML document. Of course there's a similar process to take the XML and rebuild the model of a styled document from it.

The process isn't very complicated but it does take some fiddling around with the details of javax.swing.text.Element, plus observing what you actually get out of a styled document.


Thanks for the quick reply. I really appreciate it. Anyway, I found a format called ".rtf". Seems like some has used it without any issue. What about it?
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, Thats the way..

Use RTFEditorKit in java. It will take care of everything..
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

Just wanna tell you that RTFEditorKit provided by Java doesn't support alignments, images, and some other styles. You can resolve this by downloading "AdvancedRTFEditorKit", and "jai_codec" image rendering packages. They are not from Java, but from Java-sl site
 
Henry Wessex
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can anyone show a code example, how can you save the styledDocument as an XML file?

Thanks
 
Tony Docherty
Bartender
Posts: 2988
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did a quick search and found this thread on the Oracle forums where someone has provided some code:
https://community.oracle.com/message/4994743

I've no idea if it works though.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic