单击它时,树节点图标消失

时间:2014-07-04 12:01:58

标签: java swing icons jtree treecellrenderer

enter image description here / *当我点击树节点(EX:WEBLOGIC被选中)时,节点图标消失,但其他图标(未选中)即将到来。请帮我解决这个问题。这个是一个基于摇摆的计划* /

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;
    }
  }

1 个答案:

答案 0 :(得分:0)

树中的节点可以具有自定义图标,但大多数外观在树中包含子项的节点旁边都有一个附加文件夹,如图标。我认为你的意思是当你点击它时,节点旁边的文件夹图标就会消失。如果树询问模型是否有子项并且模型返回true,则显示图标。当您单击节点并且子节点不存在时,树将删除该图标。

如果您未实施getChildren和isLeaf方法或未正确实施isLeaf,则会发生这种情况。 isLeaf方法告诉JTree UI绘制或不绘制文件夹图标。另外,请确保为您的需求设置了正确的setAsksAllowsChildren()getChildCount()会为每个节点返回正确的值。