I've seen plenty of examples for changing the icon of nodes during tree instantiation, but I'd like a way to dynamically change the icon of an individual node later. So, in my main code I add my custom renderer to my tree:
// Icon I want to set nodes to later
ImageIcon checkIcon = new ImageIcon("check.jpg");
// Creates tree with my nodes
JTree tree = new JTree(nodes.top);
// Create custom renderer
Scenario1Renderer renderer = new Scenario1Renderer();
// Set to single tree selection
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
// Set tree to my custom renderer
//renderer.setRendererIcon(greenIcon);
tree.setCellRenderer(renderer);
My code in the renderer is
public class Scenario1Renderer extends DefaultTreeCellRenderer {
ImageIcon rendererIcon;
public void setRendererIcon(ImageIcon myIcon){
this.rendererIcon = myIcon;
};
public Component getTreeCellRendererComponent(
JTree tree,
Object value,
boolean sel,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus)
{
Component ret = super.getTreeCellRendererComponent(tree, value,
selected, expanded, leaf, row, hasFocus);
//setIcon( rendererIcon ) ;
return ret;
}
So, obviously if I set my rendererIcon, it'll paint all my nodes with the icon I pass in during tree instantiation. I instead want some sort of method that can set the icon of an individual node later on in the execution of my program.