Display XML Formatted in JTextPane

Discussion in 'Java' started by Michael Jung, Apr 7, 2008.

  1. Michael Jung

    Michael Jung Guest

    Jason Cavett <> writes:
    > 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
    Michael Jung, Apr 7, 2008
    #1
    1. Advertising

  2. Michael Jung

    Jason Cavett Guest

    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
    Jason Cavett, Apr 7, 2008
    #2
    1. Advertising

  3. Michael Jung

    Mark Space Guest

    Jason Cavett wrote:
    > 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?
    Mark Space, Apr 7, 2008
    #3
  4. Michael Jung

    Jason Cavett Guest

    On Apr 7, 3:25 pm, Mark Space <> wrote:
    > Jason Cavett wrote:
    > > 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?


    Well, of course, automagic would be great, but I realize that may not
    exist. I'll look into SimpleAttributeSet.
    Jason Cavett, Apr 7, 2008
    #4
  5. Michael Jung

    Mark Space Guest

    Jason Cavett wrote:
    > On Apr 7, 3:25 pm, Mark Space <> wrote:
    >> Jason Cavett wrote:
    >>> 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?

    >
    > 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.
    Mark Space, Apr 7, 2008
    #5
  6. Michael Jung

    negrini

    Joined:
    Sep 17, 2011
    Messages:
    1
    negrini, Sep 17, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    6,346
  2. Fabrice
    Replies:
    4
    Views:
    17,377
    praitheesh
    Dec 31, 2008
  3. Henry S. Thompson

    SPAN tag doesn't display properly in JTextPane

    Henry S. Thompson, May 1, 2004, in forum: Java
    Replies:
    0
    Views:
    500
    Henry S. Thompson
    May 1, 2004
  4. Replies:
    1
    Views:
    719
  5. Ironsienna
    Replies:
    0
    Views:
    403
    Ironsienna
    Nov 29, 2006
Loading...

Share This Page