JTree application

G

giangiammy

Hi all,

I'm using a JTree to visualiza an XML file:
I create the various node with:

new DefaultMutableTreeNode("THIS IS: string to show");

I'D like to have the string separated in 2 parts,
"THIS IS" should be bold face, while
"string to show" is normal face.

Is there a way to divide that string and have it shown
with different fonts.

Or is there some other widgets to use?

(Eventually, if you have a link to an example)

thanks
giammy
 
A

Andrew T.

new DefaultMutableTreeNode("THIS IS: string to show");

I'D like to have the string separated in 2 parts,
"THIS IS" should be bold face, while
"string to show" is normal face.

Is there a way to divide that string and have it shown
with different fonts.

Most Swing components accept HTML, so you might try
something like..

new DefaultMutableTreeNode(
"<html><body><b>THIS IS:</b> string to show");

HTH
Andrew T.
 
R

Rogan Dawes

Andrew said:
Most Swing components accept HTML, so you might try
something like..

new DefaultMutableTreeNode(
"<html><body><b>THIS IS:</b> string to show");

HTH
Andrew T.

That would work as a hack, but I suspect that you really want to use a
TreeCellRenderer to do the job properly. Not meaning to dismiss Andrew's
approach, which I use below, but because I suspect that you actually
want to do something a bit more complicated than you are actually asking.

For example, extending DefaultTreeCellRenderer, you could create your
nodes using

new DefaultMutableTreeNode("string to show");

and render it with

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

JLable label = (JLabel) super.getTreeCellRendererComponent(tree,
value, selected, expanded, leaf, row, hasFocus);
label.setText("<html><body><b>THIS IS:</b> " + value.toString());
return label;
}

This is more of a building block to get you started building your own
more customised renderers.

Regards,

Rogan
 
G

giangiammy

Hi,

and thanks for the answers:
I tested both solution: the first (just put html in element node)
did not work (probably the Jtree widget do not support it - I
do not know)

Anyway with the second solution a defined MyRenderer and
works correctly: by now I just made it bold, but probably
I will add some other modification

thanks
giammy
 

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

Similar Threads

Jtree renderers 0
Extra Row in JTree 6
Updating JTree When Adding Nodes 2
Programmatically removing nodes from a JTree 2
DOM extension of JTree 2
JTree model update problem 3
JTable within JTree 1
JTree oddity... 5

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top