Display XML Formatted in JTextPane

M

Michael Jung

Jason Cavett said:
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
 
J

Jason Cavett

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
 
M

Mark Space

Jason said:
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?
 
J

Jason Cavett

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.
 
M

Mark Space

Jason said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top