-4

在此处输入图像描述 /* 当我点击树节点时(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;
    }
  }
4

1 回答 1

0

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

如果您没有实现 getChildren 和isLeaf方法或错误地实现了 isLeaf,就会发生这种情况。isLeaf 方法告诉 JTree UI 绘制或不绘制文件夹图标。还要确保setAsksAllowsChildren()根据您的需要设置正确的值,并且getChildCount()为每个节点返回正确的值。

于 2014-07-05T19:57:37.063 回答