如何在JTree java swing中显示二叉树

时间:2013-11-24 14:30:33

标签: java swing tree

我有二叉树类,当然还有节点类

public class Node {
     SomeClass somedata;
     Node leftChld;
     Node rightChld;
}

我需要在JTree swing gui组件中显示我的二进制文件。请举例说明。 Thx提前。

对不起主题,我自己做了 这是

 public void showInTree(DefaultMutableTreeNode rtCm,BNode rt){
  if (rt!=null) {
    StringBuilder str = new StringBuilder(String.valueOf(rt.numVal));
    if (rt.chVal!='\0'){
        str.append("--" + "(").append(String.valueOf(rt.chVal)).append(")");
    }
     DefaultMutableTreeNode newnode = new DefaultMutableTreeNode(str);
    rtCm.add(newnode);
    if (rt.leftBNode != null) {
        showInTree(newnode, rt.leftBNode);
}   
    if (rt.rightBNode!=null) {
        showInTree(newnode,rt.rightBNode);
    }
  }
}

1 个答案:

答案 0 :(得分:1)

这是

 public void showInTree(DefaultMutableTreeNode rtCm,BNode rt){
  if (rt!=null) {
    StringBuilder str = new StringBuilder(String.valueOf(rt.numVal));
    if (rt.chVal!='\0'){
        str.append("--" + "(").append(String.valueOf(rt.chVal)).append(")");
    }
     DefaultMutableTreeNode newnode = new DefaultMutableTreeNode(str);
    rtCm.add(newnode);
    if (rt.leftBNode != null) {
        showInTree(newnode, rt.leftBNode);
}   
    if (rt.rightBNode!=null) {
        showInTree(newnode,rt.rightBNode);
    }
  }
}