• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

dynamically changign the color of nodes in a JTree

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
reply
    Bookmark Topic Watch Topic
  • New Topic