• Post Reply Bookmark Topic Watch Topic
  • New Topic

dynamically changign the color of nodes in a JTree

 
Craig Schlenoff
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a JTree and displayed it in a JFrame. Based on data I get fed into my program, I want to dynamically change the color of certain nodes in the JTree. There is no user interaction -- everything happens inside the program.
I have the alghorithms to identify which node I want to change. What I don't know is how to "tell" the JTree to change the color of specific nodes. I believe it has something to do with the TreeCellRenderer, but I am not sure exactly how.
Thanks for any help you can provide!!!
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
schlenof,
Please change your name to be compliant with JavaRanch's naming policy.
Your displayed name should be 2 separate names with more than 1 letter each. We really would prefer that you use your REAL name.
You can change your name: here.
Thanks,
Cindy
Moving this to Swing.
 
Paul Frey
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig,
There are two ways to do it. The most obvious and cleaner way is to implement your own TreeCellRenderer. The other method involves overriding the JTree Look and Feel UI classes (not recommended by me, just pointing out the option).
 
Craig Schlenoff
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I was a bit too vague in my question. Let me try again...
I have changed an attribute in a node in a JTree. I want to tell the TreeCellRenderer to look again at this node since that attribute that was changed will effect how the node should be renderered. I tried using the nodeChanged() method, but it did not work (the colot of the node did not change). Any advise how I can do this?
Thanks!
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It could be that the node color did change, however, you did not repaint() your JTree object. You might try that method after changing the color or even validate(). I forget which one is more "SWINGee".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!