954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to build a JTree with multi-line nodes

I want to expand my Jtree nodes into multiple lines. Any idea for how to achieve this?

warlord902
Junior Poster
120 posts since Dec 2010
Reputation Points: 19
Solved Threads: 0
 
mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

Ya I already had a look there, but the example is looking bit complex for me to understand so looking for a solution in simple words, I do not want any working code to do this just want to know the process to make this happen.

warlord902
Junior Poster
120 posts since Dec 2010
Reputation Points: 19
Solved Threads: 0
 

I vote your answer as best reply in April 2011, but here isn't "lonely hearts" post your code, then we'll see

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

I've never tried this, but the nodes in JTree are rendered by default using JLabels.
You can put multi-line text in a JLabel by using simple HTML, so maybe you can use HTML to get multi-line nodes? Try a node with text like this:
line 1a
line1b<?HTML>
you never know...

Just tried it - it works.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

I've never tried this, but the nodes in JTree are rendered by default using JLabels. You can put multi-line text in a JLabel by using simple HTML, so maybe you can use HTML to get multi-line nodes? Try a node with text like this: line 1a
line1b<?HTML> you never know...

Just tried it - it works.

Yeah it is working fine. But still one problem is there.
In default look and feel of java swing it is working fine, as it automatically expand the node height

but when we switch to Windows look and feel it fails, as in that mode it do not expand the height, i was facing this problem from the morning as i'm working in windows look and feel, so failed to figure out what was going wrong.

Have any idea, how to make it work even in windows look and feel?

warlord902
Junior Poster
120 posts since Dec 2010
Reputation Points: 19
Solved Threads: 0
 

Sorry, that's not something I've tried. I guess you'll just have to keep experimenting, unless anyone else has a better idea...?

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

no, not, you have to search how to expand Node or ... programatically, nothing else, link that I posted contains example for that

to L&F --> with same result as JamesCherrill, because each L&F allways depends by Native OS runs on, sure there exist something as BasicTreeUI, MetalTreeUi ..., but againg depends by Native OS and L&F too, and Over Ride these methods are (to hard) nuts for teams of Swing Gurus


and here too http://www.exampledepot.com/egs/javax.swing.tree/pkg.html

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

no, not, you have to search how to expand Node or ... programatically, nothing else, link that I posted contains example for that

to L&F --> with same result as JamesCherrill, because each L&F allways depends by Native OS runs on, sure there exist something as BasicTreeUI, MetalTreeUi ..., but againg depends by Native OS and L&F too, and Over Ride these methods are (to hard) nuts for teams of Swing Gurus

and here too http://www.exampledepot.com/egs/javax.swing.tree/pkg.html

yeah, i am experimenting with TreeRenderer, hoping to get it done for windows L&F as well. Otherwise I will certainly need to find another way around. Anyways, thanks you gave me some bit of ideas to work on.

warlord902
Junior Poster
120 posts since Dec 2010
Reputation Points: 19
Solved Threads: 0
 

Please let us know how you get on - especially if you find a way to get it under Win L&F
J

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 
:-) answered http://forums.oracle.com/forums/thread.jspa?threadID=2215528&tstart=0

That is my thread that I posted there just an hour before :-)

I thought to give a try to Oracle forums, and it worked ,now working fine
I just forgot to give a try to rowHeight() :-)

warlord902
Junior Poster
120 posts since Dec 2010
Reputation Points: 19
Solved Threads: 0
 

Please tell us exactly what it was you had to do with rowHeight
thanks
J

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
Please tell us exactly what it was you had to do with rowHeight thanks J

In windows LAF row height is default set to around 15 or 18, so now I just increased the row height of my Jtree to say jTree.setRowHeight(28) depending upon how many rows I want to show in my nodes.

warlord902
Junior Poster
120 posts since Dec 2010
Reputation Points: 19
Solved Threads: 0
 

OK, thanks
cheers
J

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

I must "be quite" again, but then I 2nd. times *** you,

majorities of Forums required be forgive on Crossposting,

you can crosspost your question how many times as you wants, but you have to add link to another Forum(s) to your question, its Saturday and so ...

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

I must "be quite" again, but then I 2nd. times *** you,

majorities of Forums required be forgive on Crossposting,

you can crosspost your question how many times as you wants, but you have to add link to another Forum(s) to your question, its Saturday and so ...

This is nice of you, i'm new to forums so not much aware of rules and traditions here, I will keep good care of it in future. Thanks

warlord902
Junior Poster
120 posts since Dec 2010
Reputation Points: 19
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: