Re: HTML in JTree (via JE
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