This week's giveaway is in the Threads forum.
We're giving away four copies of Java Concurrency Live Lessons and have Doug Schmidt on-line!
See this thread for details.
Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

highlighting files  RSS feed

 
tomo black
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,i have a JTree that displays the contents of a directory but i would like to know how to highlight each file type using different colors. e.g. .class is red, .java is yellow etc.
thanks for your help.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Swing forum.
 
Claudia Neri
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know how to do this??? Please help! Thanks
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll have to make your own TreeCellRenderer... you can just extend DefaultTreeCellRenderer and override the following method :


The value parameter holds whatever data the given cell holds... either an actual File reference or just a String, depending on how you have done your model. Either way, your going to get the file name and parse it to get the extension and set the tree cell renderer background to whatever color you want. You could either hard-code what color goes with what extension directly into the tree cell renderer, or you could add a HashMap and use the extension as the key and the color as the value in the HashMap. This would allow you to dynamically change the color you want an extension to render as, or add new extension-color mappings on the fly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!