Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Custom Icons in JTree

 
Jeremy Hooie
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can implement a TreeRenderer and use the getTreeCellRendererComponent to specify the icon of each node.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic