File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA 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

Joined: Dec 14, 2011
Posts: 29

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: 19684

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
I agree. Here's the link:
subject: How to set Icons in the JTree