Changing specific TreeNode icons
Hi,
is it possible to change the icons of specific Leafs? In my case i want that some Leafs hava an "abortIcon" and others an "aproveIcon".
Info: Those icons will have to be changed during the run of my program...
Do i have to extend "DefaultTreeCellRenderer" ?
Cant i just specifiy the icons somehow in "DefaultMutableTreeNode"?
Thanks for your help!
Greetings
Martin
[417 byte] By [
ErlemanMa] at [2007-11-26 19:11:34]

# 1
> Hi,
>
> is it possible to change the icons of specific Leafs?
> In my case i want that some Leafs hava an "abortIcon"
> and others an "aproveIcon".
>
> Info: Those icons will have to be changed during the
> run of my program...
>
> Do i have to extend "DefaultTreeCellRenderer" ?
You don't have to, but it's the easiest way to achieve what you want.
>
> Cant i just specifiy the icons somehow in
> "DefaultMutableTreeNode"?
You can, but you would still need a custom renderer. It can be as simple as this:
===================
import javax.swing.*;
import java.swing.tree.*;
import java.awt.*;
public class MyTreeCellRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent (JTree tree,
Object value,boolean sel, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded,
leaf, row, hasFocus);
if(value instanceof MySpecialNodeClass) {
MySpecialNodeClass myNode = (MySpecialNodeClass )value;
Status status = myNode.getStatus();
Icon nodeIcon = null;
switch(status) {
case ABORTED:
icon = ICON_ABORTED;
break;
case APPROVED:
icon = ICON_APPROVED;
break;
case SOME_OTHER_STATUS:
//...
break;
}
setIcon(icon);
}
return this;
}
}
The renderer would have private final static variables for the ICON_XXX items. Your node class would have a getStatus() method that returned the appropriate value from a Status enum.
Jim S.