Is it possible to assign different icons to different nodes in a JTree using DefaultTreeCellRenderer.setOpenIcon()? Thanks.
Is it possible to assign different icons to different nodes in a JTree using
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
getTreeCellRendererComponentmethod, decide which icon to display based on the value passed to the method, and callsetIcon.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).