javaswingiconsjtreetreecellrenderer

Tree node icon dissappears when i click on it


enter image description here /* when i am clicking the tree node (EX:WEBLOGIC is selected) the node icon disappears,but the other icons(Non selected) are coming.please help me out to solve this issue.This is a swing based program*/

class ColorRenderer extends DefaultTreeCellRenderer
{
  public ColorRenderer()
  {
    super();
  }

  public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
  {
    try
    {
      Component cell = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
      RTGTreeNode treeNode = (RTGTreeNode) value;
      GraphUniqueKeyDTO graphUniqueKeyDTO = treeNode.getGraphUniqueKeyDTO();

      /*Default Icon not needed.*/
      setIcon(null);

      if (graphUniqueKeyDTO == null)
      {
        return cell;
      }

      String nodeName = treeNode.toString();

      if (!leaf)
      {
        cell.setFont(NSColor.mediumPlainFont());
        if (selected)
        {
          cell.setForeground(Color.black);
          cell.setBackground(new Color(128, 0, 0));
        }
        else
        {
          Color color = treeNode.getNodeColor();

          if (treeNode.getTreeViewToolTip() != null)
            nodeName = treeNode.getTreeViewToolTip();
          openIcon = treeNode.getImgIcon();
          if(openIcon!=null){
              setIcon(openIcon);
              setLeafIcon(openIcon);

          }


          if(color == null)
            cell.setForeground(NSColor.leftPaneGroupTitleColor());
          else
            cell.setForeground(color);
        }
      }
      else
      {
        cell.setFont(NSColor.smallPlainFont());
        if (selected)
        {
          cell.setForeground(Color.black);
          cell.setBackground(new Color(128, 0, 0));
        }
        else
        {
          cell.setForeground(NSColor.leftPaneGraphTitleColor());
        }
      }

      setToolTipText(nodeName);
      JLabel currentCell = (JLabel) cell;
      currentCell.setHorizontalAlignment(JLabel.CENTER);
      return cell;
    }
    catch (Exception ex)
    {
      Log.errorLog("ColorRenderer", "getTreeCellRendererComponent", "", "", "Exception - " + ex);
      return null;
    }
  }

Solution

  • A node in a tree can have a custom icon but most skins have an additional folder like icon next to those nodes in a tree that contain children. I think you mean the Folder icon next to the node disappears when you click on it. If the tree asks the model if it has children and the model returns true then the icon is displayed. When you click on the node and the children are not present the tree removes the icon.

    This will happen if you did not implement the getChildren and isLeaf method or implemented isLeaf incorrectly. The isLeaf method tells the JTree UI to draw or not draw the folder icon. Also make sure setAsksAllowsChildren() is set the the correct value for your needs and getChildCount() returns the correct value for each node.