title: Set icon to each node in Jtree
body: <p>I want to set for each node in my JTree a different icon, actually I'm loading each node from a data base, with a "while", I set each icon like a root, leaf or parent. Like this:</p>
  
  <p>All my declarations are global:</p>
  
  <pre><code>private ResultSet myResultSet;
  protected DefaultTreeModel treeModel;
  private DefaultMutableTreeNode rootNode,childNode,parent1,parent2;
  </code></pre>
  
  <p>And this is the code where I set my nodes:</p>
  
  <pre><code>myResultSet=rtnNodes(); /*Method that returns a RS with my nodes*/
  while(myResultSet.next()){
    switch(myResultSet.getInt(1)){  /*The first column is the type of node: root, parent, leaf...*/
      case 0: treeModel = new DefaultTreeModel((rootNode=new DefaultMutableTreeNode(myResultSet.getString(2)))); break;  /*root node*/
      case 1: case 4: parent1 = parent2 = makeNode(rootNode); break;  /*parent node*/
      case 2: makeNode(parent2); break;  /*leaf node*/
      case 3: parent2 = makeNode(parent1); break;  /*sub patern node*/
    } /*makeNode is the method where I create the nodes*/
  }
  </code></pre>
  
  <p>The method makeNode is this:</p>
  
  <pre><code>public DefaultMutableTreeNode makeNode(DefaultMutableTreeNode parent){
    //The second column in the RS is the name of the node
    treeModel.insertNodeInto((childNode=new DefaultMutableTreeNode(myResultSet.getString(2))),parent,parent.getChildCount());
    return childNode;
  }
  </code></pre>
  
  <p>After to fill the treemodel with my nodes, I set the model to my JTree:</p>
  
  <pre><code>myJTree.setModel(treeModel);
  myJTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
  </code></pre>
  
  <p>But the problem is. when I try to set the icons. I create a subclass called myTreeRenderer, and I use this:</p>
  
  <pre><code>myJTree.setCellRenderer(new treeRenderer());
  </code></pre>
  
  <p>But it doesn't set the icons as I want, the subclass is:</p>
  
  <pre><code>private ImageIcon root,parent,leaf;
  
  public myTreeRenderer() {
     root=setIcons(2);  /*setIcons is a method that I dont publish in this post, that helps me to set the path of the icons*/
     parent=setIcons(3);
     leaf=setIcons(4);
  }
  
  @Override
  public Component getTreeCellRendererComponent(JTree tree,Object value,boolean selected,boolean expanded,boolean leaf,int row,boolean hasFocus){               
     super.getTreeCellRendererComponent(tree,value,selected,expanded,leaf,row,hasFocus);
     DefaultMutableTreeNode nodo = (DefaultMutableTreeNode)value;
     TreeNode t = nodo.getParent();
     if(t!=null){
        setIcon(root);
     }
     return this;
  }
  </code></pre>
  
  <p>How I can set the icon for each node without using his name? The code of the subclass, as is, set all the nodes with the same icon, and each time I selected a node in the jtree, the getTreeCellRendererComponent runs, I don´t want this.</p>