This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to set Icons in the JTree

 
William Flores
Ranch Hand
Posts: 32
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys!

I have a problem here. I'm implementing a JTree, but now I need set icons in each node.
I have searched about this, but I don't undestand very well how to do this.
Can someone help me? Please!
 
Nitin Surana
Ranch Hand
Posts: 129
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All you need is a TreeCellRender.

I have posted a very simple example for you here

http://www.onlinexamples.com/showfullexample.action?idexamples=82&title=Jtree%20Nodes%20With%20Icon%20-%20Treecellrenderer
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no need to override DefaultTreeCellRenderer. It has its own methods to set the icons for opened nodes, closed nodes and leaf nodes. Just create an instance, set the right icons, and set that instance as the renderer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic