The behaviour you are describing is caused by the fact that you are not setting correctly the node icon in the switch
statement. The setOpenIcon()
, setClosedIcon()
etc set the icons that the renderer will use for the whole tree. So the action taken in the last node is going to decide what icons will be rendered at the end.
The DefaultTreeCellRenderer
extends a JLabel
. This component will be used for each node to render its contents. It is this components' icon that needs to be set i.e. your code should look like this:
switch (error) {
case "CORRECT":
setIcon(leafIcon); // sets the icon of the renderer which is a JLabel
break;
case "CREATE":
setIcon(gifNew);
break;
case "DELETE":
setIcon(gifError);
break;
...
}
Be sure to set the icon in all cases though and make sure that you understand how the renderer is used to render tree nodes (the same instance is reused for all nodes)