Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1802 > unrolled thread

Re: HTML in JTree (via JE

Started by"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
First post2011-04-27 15:35 +0000
Last post2011-04-27 15:36 +0000
Articles 10 — 5 participants

Back to article view | Back to comp.lang.java.gui

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: HTML in JTree (via JE "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
    Re: HTML in JTree (via JE "Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
      Re: HTML in JTree (via JE "Johannes Schwall" <johannes.schwall@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
        Re: HTML in JTree (via JE "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
          Re: HTML in JTree (via JE "Johannes Schwall" <johannes.schwall@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
            Re: HTML in JTree (via JE "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
              Re: HTML in JTree (via JE "Johannes Schwall" <johannes.schwall@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
                Re: HTML in JTree (via JE "Johannes Schwall" <johannes.schwall@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
                  Re: HTML in JTree (via JE "preetam.purbia" <preetam.purbia@THRWHITE.remove-dii-this> - 2011-04-27 15:36 +0000
                    Re: HTML in JTree (via JE "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:36 +0000

#1802 — Re: HTML in JTree (via JE

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: HTML in JTree (via JE
Message-ID<7394ab35c63ab@uwe>
  To: comp.lang.java.gui
Johannes Schwall wrote:
..
>Is there any way to have HTML-formatted text in a JTree node? 

Yes.

>..I have to display up to 400 characters in the node.

I do not see how that is directly relevant to HTML.

>I was thinking about building a JEditorPane into a TreeNode, but have no
>idea how to do this.

Most JComponents will render HTML.  I guess a JLabel 
might be better suited to this, than a JEditorPane.
For further details, consult the Java Tutorial..
<http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#display
>

-- 
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200706/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#1803

From"Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
Message-ID<466ee0d2$0$8717$ed2619ec@ptn-nntp-reader02.plus.net>
In reply to#1802
  To: comp.lang.java.gui
Andrew Thompson wrote:
> Johannes Schwall wrote:
> 
>> ..I have to display up to 400 characters in the node.
> 
> I do not see how that is directly relevant to HTML.

It's indirectly relevant as JLabel still can't wrap plain text. I don't 
off hand remember if JTree handle nodes of different heights.

>> I was thinking about building a JEditorPane into a TreeNode, but have no
>> idea how to do this.
> 
> Most JComponents will render HTML.  I guess a JLabel 
> might be better suited to this, than a JEditorPane.

Yes, but it is incredibly slow. Every time a cell painter is used the 
HTML gets parsed, a model of the HTML is built and then rendered. It's 
an inherently slow process. The implementation is distinctly suboptimal. 
Doing it many times does not help.

There is a workaround to cache the JLabels, but it is quite ugly.

Tom Hawtin

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1816

From"Johannes Schwall" <johannes.schwall@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
Message-ID<f4o1tm$s47$1@sagnix.uni-muenster.de>
In reply to#1803
  To: comp.lang.java.gui
>>> ..I have to display up to 400 characters in the node.
>> 
>> I do not see how that is directly relevant to HTML.
> 
> It's indirectly relevant as JLabel still can't wrap plain text. I don't
> off hand remember if JTree handle nodes of different heights.

Yes, indeed, that's one of the reasons. Another is that I need to set some
of the text in different colours.

>>> I was thinking about building a JEditorPane into a TreeNode, but have no
>>> idea how to do this.
>> 
>> Most JComponents will render HTML.  I guess a JLabel
>> might be better suited to this, than a JEditorPane.
> 
> Yes, but it is incredibly slow. Every time a cell painter is used the
> HTML gets parsed, a model of the HTML is built and then rendered. It's
> an inherently slow process. The implementation is distinctly suboptimal.
> Doing it many times does not help.
> 
> There is a workaround to cache the JLabels, but it is quite ugly.

So ... what does that mean for me: Is both (JLabel and JEditorPane) possible
to use as a node?

And what would be the way to do this? I guess I will have to use the
CellRenderer somehow, but I'm not really sure how. Is there a tutorial or
example on how to use other components as TreeNodes?

Again, many thanks.

Johannes Schwall

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1817

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
Message-ID<739ffbc129de2@uwe>
In reply to#1816
  To: comp.lang.java.gui
Johannes Schwall wrote:
..
>And what would be the way to do this? 

I suggest(ed) consult the Java Tutorial.

>...I guess I will have to use the
>CellRenderer somehow, but I'm not really sure how. Is there a tutorial or
>example on how to use other components as TreeNodes?

As linked in my first reply.

-- 
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200706/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1818

From"Johannes Schwall" <johannes.schwall@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
Message-ID<f4o4rh$sl1$1@sagnix.uni-muenster.de>
In reply to#1817
  To: comp.lang.java.gui
> I suggest(ed) consult the Java Tutorial.

Sorry, of course, you are right. And I did take a look at it - in fact, I
had done so before, as my tree already has some nodes with individual
icons, just as presented there.

My problem is, that I do not really understand how to create a node that is
not only a node, but has the ability described before (text with
linebreaking and the possibility to format parts of the text).

I assume, it can be done by telling the CellRenderer not to return a
DefaultMutableTreeNode, but a new Object, call it TextNode ... so, how to
create this TextNode?

Something like the following would produce a normal node, which would
probably use a JLabel (I guess that's the default behaviour).


public class TextNode extends DefaultMutableTreeNode {

 private String text;
        
  public TextNode() {
    this.text = "<html><head></head><body>Formatted Text.</body></html>";
  }

  public void setText(String text) {
    this.text = text;
  }
        
  @Override
  public String toString() {
    return this.text;
  }

}

I did not test this yet. But if I remember correctly, I had difficulties
using HTML inside the node like this. Thus my thrive to use some different
element, e.g. JEditorPane for the HTML rendering.

Johannes

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1819

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
Message-ID<73a0a7ca1bf2a@uwe>
In reply to#1818
  To: comp.lang.java.gui
Johannes Schwall wrote:
..
>My problem is, that I do not really understand how to create a node that is
>not only a node, but has the ability described before (text with
>linebreaking and the possibility to format parts of the text).

How does the new renderer understand where to
put the breaks?  What formats to use?

>Something like the following would produce a normal node, which would
>probably use a JLabel (I guess that's the default behaviour).
..
>I did not test this yet. But if I remember correctly, I had difficulties
>using HTML inside the node like this. 

Perhaps it is better to break the problem into parts..
- How to get a JComponent to display HTML
- How to get the renderer to understand the HTML
formatting that needs to be performed.
- How to get JTree to use the component.

>...Thus my thrive to use some different
>element, e.g. JEditorPane for the HTML rendering.

JEP would also be possible, but its default behaviour
is to allow editing, and it has many features not required
to simply render HTML.

In any case, you were on the right track with your 
example, hopefully this will get you further..
<sscce>
import javax.swing.*;
import java.awt.BorderLayout;

class BigRedText {

  public static void main(String[] args) {
    String message1 = "Big Red Text";

    String message2 = "<font size='+2'>Big</font> " +
      "<font color='red'>Red</font><br>Text";
    JPanel mainPanel = new JPanel( new BorderLayout() );

    JLabel labelNoHTML = new JLabel(message1);
    mainPanel.add( labelNoHTML, BorderLayout.NORTH );

    JLabel labelAsHTML = new JLabel("<html><body>" + message1);
    mainPanel.add( labelAsHTML, BorderLayout.CENTER );
    // ..it looks EXACTLY the same (why wouldn't it?)

    // Now our String with HTML formatting
    JLabel labelWithHTML = new JLabel("<html><body>" + message2 );
    mainPanel.add( labelWithHTML, BorderLayout.SOUTH );

    JOptionPane.showMessageDialog(null, mainPanel);
  }
}
</sscce>

HTH

-- 
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200706/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1821

From"Johannes Schwall" <johannes.schwall@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
Message-ID<f4oefd$v6r$1@sagnix.uni-muenster.de>
In reply to#1819
  To: comp.lang.java.gui
Thanks a lot, I'm nearly there.

The nodes themselves look quite nice now. Just two problems remain:

1. The nodes (or rows) seem to have the correct height, but the text sits at
the bottom and is being cropped halfway through the second line.
My "standard" nodes with one line are ok.

2. They are a little too wide.

I've been looking into a solution for the first problem, and think I have to
apply JComponent.setAlignmentY(float), but ... (a) it does not work as I
think it should (see below), and (b) there seems to be a little confusion
in the API documentation: The doc at
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JComponent.html#setAlignmentY(float)
says that the Y-alignment is for horizontal alignment, whereas X-alignment
is for vertical alignment ... shouldn't it be the other way round?

Anyway, to be on the save side, I have set them both:

public class SentenceTreeRenderer extends DefaultTreeCellRenderer {

  public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row, boolean
hasFocus) {

    Component c = super.getTreeCellRendererComponent(tree, value, selected,
expanded, leaf, row, hasFocus);
                
    tree.setRowHeight(0);
    tree.setAlignmentX(Component.TOP_ALIGNMENT);
    tree.setAlignmentY(Component.TOP_ALIGNMENT);

    //... setting icons etc.

    return c;

  }

}

Unfortunately, this does not have any effect at all.

Concerning the width of the rows (my second problem) I'm currently at a
loss. As there is now method called setRowWidth(float), I'll proably have
to restrict the size of the wrapping component to prevent the surrounding
scrollpane from scrolling horizontally?

Johannes

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1823

From"Johannes Schwall" <johannes.schwall@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
Message-ID<f4olq4$1ha$1@sagnix.uni-muenster.de>
In reply to#1821
  To: comp.lang.java.gui
In case anyone should have the same problem: I just found the solution for
correctly aligning the text in the tree nodes. I was looking in the tree
and the node instead of really focussing on the Renderer; just do the
following in your TreeCellRenderer:

  setVerticalTextPosition(SwingConstants.TOP);

Johannes

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2012

From"preetam.purbia" <preetam.purbia@THRWHITE.remove-dii-this>
Date2011-04-27 15:36 +0000
Message-ID<1184139398.132010.101270@g37g2000prf.googlegroups.com>
In reply to#1823
  To: comp.lang.java.gui
send
me the example code of JEditorPane for a web hyperlinks

preetam
http://purbia.50webs.com

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2017

From"Roedy Green" <roedy.green@THRWHITE.remove-dii-this>
Date2011-04-27 15:36 +0000
Message-ID<lr8b93pejagfbuoonpa5438rsac9004hg5@4ax.com>
In reply to#2012
  To: comp.lang.java.gui
On Wed, 11 Jul 2007 00:36:38 -0700, preetam.purbia@gmail.com wrote,
quoted or indirectly quoted someone who said :

>send
>me the example code of JEditorPane for a web hyperlinks

There is a culture clash here. Even requests to domestic help use the
word "please" in North America.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web