CROSP
CROSP

Reputation: 4617

How to display BinaryTree in JTree java swing

I have binary tree class and certainly node class

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

I need to show my binary in JTree swing gui component . Please give example hot to do this. Thx in advance .

Sorry for topic, I have done it by myself Here it is

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

Upvotes: 1

Views: 946

Answers (1)

CROSP
CROSP

Reputation: 4617

Here it is

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

Upvotes: 1

Related Questions