I have the following code
class BinaryTree{
Node root;
public void inOrderTraverseTree(Node node) {
if (node!= null) {
// Traverse the left node
inOrderTraverseTree(node.leftChild);
// Visit the currently focused on node
System.out.println(node);
// Traverse the right node
inOrderTraverseTree(node.rightChild);
}
}
public void preorderTraverseTree(Node node) {
if (node!= null) {
System.out.println(node);
preorderTraverseTree(node.leftChild);
preorderTraverseTree(node.rightChild);
}
}
public static void main(String[] args) throws SQLException {
BinaryTree theTree = new BinaryTree();
//retrieve data from database, create tree, call functions, everything //works-It shows well at console
theTree.preorderTraverseTree(theTree.root);
theTree.inOrderTraverseTree(theTree.root);
}
and the second class
class Node{
Node leftChild;
Node rightChild;
Node(int id,int search,int left,int right,String name) {
this.left=left;
this.right=right;
this.id = id;
this.search=search;
this.name = name;
}
//get and sett methods
}
Which is my problem. Well console application works.I get input from DATABASE USING JDBC. The inorder, preorder... shows well, verified by paper. My problem is: -I can not print results in a jTextArea from a void method.
What I have tried to do:
- Print the results with writeObject to a file and the show them in jTextArea(fail)
- Try to convert to String (fail)
- transform void method to a string one(returning null-fail again).I guess it is recursive and I don`t do it OK
Any tips? How can I print the result of this method "theTree.preorderTraverseTree(theTree.root)" into a jTextArea?
Edit: I did declare a String/Object and tried to parse the value from the method into it, but without succes. Also I don't know why inside a void method setText method is not working.
public void preorderTraverseTree(Node node) {
if (node != null) {
System.out.println(node);
jTextArea1.setText(node.toString());
preorderTraverseTree(node.leftChild);
preorderTraverseTree(node.rightChild);
}
}
//and the button
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Node theTree;
theTree.preorderTraverseTree(theTree.root);
}
My question: Inside the preorderTraverseTree, why .setTextMethod doesn't work.
Why do I need this? Well, I wont console output to be in a jTextArea