• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to set empty border for every cell in JTree?  RSS feed

 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I can't anyhow set empty border in my cell renderer that extends DefaultTreeCellRenderer.

This does not work! I have no empty space around nodes. However if I use dashed border this code works and I see dashed, colored border around every node.

How to set empty border around every node in JTree?
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Resolved!

JTree has method setRowHeight. It is needed to set row height to zero(setRowHeight(0)) and then empty border for DefaultTreeCellRenderer will work.
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- no idea why your decision is reseting setRowHeight, its about wrong decision

- because Borders added to Component, JComponent and JLabel works as I expected
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because Borders added to Component, JComponent and JLabel works as I expected

They work as expected but not in case of my own tree cell renderer that extends DefaultTreeCellRenderer.

But I have another problem.
Unfortunately after I expand child of root of tree then plus icon does not appear next to icon.
However plus/minus icon correctly apppears for all nodes except children of tree root.
My code to create JTree is :

In method retrieveExistentConnectionsNodes(root); I only insert new nodes like so :

Notice I have empty border around my tree
setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 0));.
But if I remove this border it makes no difference.
I hide root of tree by setRootVisible(false).

Where is plus icon next to child of hidden root?
Although I expand and collapse this child there is no plus minus icon.

Thank you!
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
of my own tree cell renderer that extends DefaultTreeCellRenderer.


sound like as

- you love guessing, wild shots to dark without an SSCCE

- Icon in JLabel by default returns proper workaround, remove any code that block

- dont put JComponents to JTree or to use proper LayoutManager instead
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you @mKorbel that you reply me!

You may copy paste the following code to see my issue:

As you see, despite that "parent label" has children it does not have plus icon next to it. Also notice that "child label" has plus icon because it has child.
Try to comment this line and you will see that plus icon is not available for tree root which is visible but plus icon is lready present in "parent label"


Try to comment this line

Now run main method an you see that there is no space between nodes as we had before due to BadTreeCellRenderer.

Thank you @mKorbel !
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!