Is it possible to assign different icons to different nodes in a JTree
using DefaultTreeCellRenderer.setOpenIcon()
? Thanks.
1 Answers
The same cell renderer instance is used to render all the cells of the tree. The open icon is the little + symbol, or triangle symbol at the left of every tree node which allows to expand it (i.e. see its child nodes). I doubt this is the icon you want to change. It would be rather strange not to use the same one for all the nodes.
If you want to display a custom icon for a specific node, create a subclass of DefaultTreeCellRenderer, override the getTreeCellRendererComponent
method, decide which icon to display based on the value passed to the method, and call setIcon
.
See http://download.oracle.com/javase/tutorial/uiswing/components/tree.html#display for a similar example (which customized the tooltip, and not the icon, but the idea is the same).

- 678,734
- 91
- 1,224
- 1,255
-
1See also [`TreeIcon`](http://stackoverflow.com/questions/4640818/change-jtree-node-icons-according-to-the-depth-level/4641530#4641530). – trashgod Aug 27 '11 at 17:32
-
Thank you both JB Nizet and trashgod – FadelMS Aug 27 '11 at 19:19