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

Different height of JTree nodes

I want to make node heights of JTree nodes to differ. I mean like the parents have different node height and childs have different node height.

I tried to put myJTree.setRowHeight(30) while adding nodes but it doesn't work because it does not work for a single node but for the whole tree. Is there any way to get it done?

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

You could subclass TreeCellRenderer and use setCellRenderer(javax.swing.tree.TreeCellRenderer) to draw each row with any arbitrary height, and setRowHeight(0) so the the current cell renderer is queried for each row's height.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
You could subclass TreeCellRenderer and use setCellRenderer(javax.swing.tree.TreeCellRenderer) to draw each row with any arbitrary height, and setRowHeight(0) so the the current cell renderer is queried for each row's height.

How can I draw each node with an arbitrary height in customTreeCellRenderer? I can't understand, I was thinking that the JTree handles node height issues. Please elaborate a bit.

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

You'll have to Google for details, but briefly, JTree uses a JLabel to draw each cell in the tree. But you can define your own renderer (normally a subclass of JLabel) to draw each cell however you like - eg use a bigger font for some cells, add graphics etc etc

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
You'll have to Google for details, but briefly, JTree uses a JLabel to draw each cell in the tree. But you can define your own renderer (normally a subclass of JLabel) to draw each cell however you like - eg use a bigger font for some cells, add graphics etc etc

Yeah, I know that and I am already using a customRenderer which implements its own JLabel for nodes but if the height of that JLabel is not within the height set for JTree rows or u can say nodes then it will not be visible completely. So we need to set the height of JTree nodes which ultimately gets reflected for all nodes.

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

This is what the API doc says:
public void setRowHeight(int rowHeight)
Sets the height of each cell, in pixels. If the specified value is less than or equal to zero the current cell renderer is queried for each row's height.
So, like I said in my first post, setRowHeight(0)

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

This is what the API doc says:

So, like I said in my first post, setRowHeight(0)

awesome...it works. I initially set the rowHeight to 0 and all nodes themselves adjusted their heights.

Thanks :)

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

@JamesCherrill

don't sub_class no, nothing, never inside Renderer, as you correctly said Renderer returns by default JLabel (on Edit JTextField), then why overrode JLabel by using JLabel, for those .... is there Editor

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

@warlord902 is you refuse Sub_Classing and setSize(), then Renderer fill HEIGHT & WEIGHT by default

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

This question has already been solved

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