• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to save a "Styled" document

 
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

Thanks
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic