Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2008

Tip: Looking for answers? Try searching our database.

Display XML Formatted in JTextPane

Thread view: 
Jason Cavett - 07 Apr 2008 18:38 GMT
I'm displaying XML in a JTextPane.  I was wondering if there was a way
to display the XML formatted with color/text/etc.  (Basically, I'm
wondering if I can associated an XML style.)  I've searched a bit, but
I haven't been able to get anything to work.

Thanks
Michael Jung - 07 Apr 2008 18:25 GMT
> I'm displaying XML in a JTextPane.  I was wondering if there was a way
> to display the XML formatted with color/text/etc.  (Basically, I'm
> wondering if I can associated an XML style.)  I've searched a bit, but
> I haven't been able to get anything to work.

You can transform XML via XSL into (X)HTML and then display it in the
JTextPane.  The bit with setting the page without having an URL is
somewhat awkward.  I asked a similar question a while ago and got no
answer.  I looked at the relevant JDK source code and couldn't really
make out the reason for the different working of setPage(URL) and
setText(String). I rather wrote a URL handler for the "string:"
protocol and registered that.  Only a few lines.

Hope that helps.
Michael
Mark Space - 07 Apr 2008 20:25 GMT
> I'm displaying XML in a JTextPane.  I was wondering if there was a way
> to display the XML formatted with color/text/etc.  (Basically, I'm
> wondering if I can associated an XML style.)  I've searched a bit, but
> I haven't been able to get anything to work.
>
> Thanks

I don't know any way to do it automagically.  Are you ok with doing it
manually via SimpleAttrributeSet and such like?
Jason Cavett - 07 Apr 2008 21:59 GMT
> > I'm displaying XML in a JTextPane.  I was wondering if there was a way
> > to display the XML formatted with color/text/etc.  (Basically, I'm
[quoted text clipped - 5 lines]
> I don't know any way to do it automagically.  Are you ok with doing it
> manually via SimpleAttrributeSet and such like?

Well, of course, automagic would be great, but I realize that may not
exist.  I'll look into SimpleAttributeSet.
Mark Space - 07 Apr 2008 23:38 GMT
>>> I'm displaying XML in a JTextPane.  I was wondering if there was a way
>>> to display the XML formatted with color/text/etc.  (Basically, I'm
[quoted text clipped - 6 lines]
> Well, of course, automagic would be great, but I realize that may not
> exist.  I'll look into SimpleAttributeSet.

Ah, I assume you knew about SimpleAttributeSet and were hoping for
something easier.

You can use the SimpleAttributesSet for the Document class to style text
for a JTextPane.  Use JTextPane.getDocument to append/change the
document to add text to an existing JTextPane.

Unlike JEditorPane you should not need a URL for this or use setPage().

There's an example in _Learning Java_.  (How many times to I have to
mention this book? ;-))

Basically, after creating the JTextPane, use

Document d = JTextPane.getDocument();
d.insertString( int position, String, AttributeSet );

to insert colored or bold/italics text.  Substitute variables where
appropriate where I have classes.

To make a simple attribute set, use something like

SimpleAttributeSet redstyle = new SimpleAttributeSet();
StyleConstants.setForeground( redstyle, Color.red );

etc.

SimpleAttributeSet is a subclass of AttributeSet (insertSring, above) so
you can just drop "redstyle" in for that parameter, for example.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.