Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Jtree Ugly Look

 
Nelson David
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am having problem with JTree, i have changed the background tree color to pink and when i expand any tree, it creates a white box equal to the size tree expanded, how should I prevent this so that the background color remains pink.
regards,
Nelson
 
Bill Liteplo
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On what platform are you running? (I have seen funky color behavior on some Unices.)
Bill

pink?!
 
Nelson David
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Win2000, I think its not the platform problem, the entire portion that turn white must be turn into the color of background(pink) rather then white.
Hopes for a good reply that solves my problem.
Nelson.
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JTree seems to be opaque as JLabels are (I mean, their background is transparent). either you make them opaque, using setOpaque(boolean) or you change the background of the underlying component.
(if changing the opaque state seems not to work, I might have gotten it wrong, and opaque means that it's not transparent, than set it opaque instead.)
chantal
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want all your JTrees in your application to look this way ( or if there is only one, and you don't care.) There is a really simple way to do this... Change the colors used by the UIManager for trees!

Before you show your tree, put new Color values into the UIManager... The keys you will probably be most interested in are:

Tree.background
Tree.foreground
Tree.selectionBackground
Tree.selectionBorderColor
Tree.selectionForeground
Tree.textBackground
Tree.textForeground

You can use them like this...

 
Nelson David
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Nathan I got my problem solved.
Nelson
 
Nelson David
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nathan is there a way to change the color of tree node?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you mean by "node"? Do you mean each entry in the tree?

The "Tree.textBackground" and "Tree.textForeground" properties control the background and foreground colors of the tree entries... but it does so for all tree entries. If you need different entries to have different colors, you will need to make a new TreeCellRenderer (probably by extending DefaultTreeCellRenderer), overriding the getTreeCellRendererComponent() method to apply different colors to the entry based on the 'value' parameter, and setting it on your tree. You can find easy examples of extending DefaultTreeCellRenderer on this site or many others, just by searching for it...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!