aspose file tools
The moose likes Swing / AWT / SWT and the fly likes How to set Icons in the JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Groovy Fundamentals video training course this week in the Groovy forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to set Icons in the JTree" Watch "How to set Icons in the JTree" New topic

How to set Icons in the JTree

William Flores
Ranch Hand

Joined: Dec 14, 2011
Posts: 30

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

Joined: Jan 21, 2011
Posts: 129

All you need is a TreeCellRender.

I have posted a very simple example for you here
Rob Spoor

Joined: Oct 27, 2005
Posts: 19990

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.

How To Ask Questions How To Answer Questions
It is sorta covered in the JavaRanch Style Guide.
subject: How to set Icons in the JTree