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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Custom Icons in JTree" Watch "Custom Icons in JTree" New topic
Author

Custom Icons in JTree

Jeremy Hooie
Ranch Hand

Joined: Aug 22, 2001
Posts: 38
Is there a way to place custom icons in a JTree to replace the standard folders and pages?
Thanks,
Jeremy
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I know how to change the Leaf Icon.
myDefaultTreeCellRenderer.setLeafIcon(new ImageIcon("somefile.gif"));
Probably similar for the nodes and root.

------------------
Happy Coding,
Gregg Bolinger


GenRocket - Experts at Building Test Data
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Ok, to set the Open Folder Icon you do the same except, setOpenIcon(new ImageIcon());
and the setClosedIcon(new ImageIcon()) sets the closed folder Icon.

------------------
Happy Coding,
Gregg Bolinger
Jeremy Hooie
Ranch Hand

Joined: Aug 22, 2001
Posts: 38
I'm setting everything as a DefaultMutableTreeNode. I execute a SQL statement and step through the ResultSet to add each element as a node. Then I create a tree model passing that node to the constructor. Is there a way to change the icon of a node so that I don't have to recode the tree using leafs?
Jeremy
Matt Hansen
Ranch Hand

Joined: Jul 31, 2001
Posts: 34
You can implement a TreeRenderer and use the getTreeCellRendererComponent to specify the icon of each node.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Custom Icons in JTree