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?

mKorbel commented: good question +9

Recommended Answers

All 8 Replies

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.

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.

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

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.

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)

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 :)

@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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.