N
Newbie
Hi,
I have this assement for college that i have completed in building a "binary
tree" using recursion.
It prints fine through the "System.out.println" command and shows each level
as its built
How can I achieve the same results for my swing textArea as it is displayed
through the above command?
This is not part of the assement but more for my own learning process of
displaying things for the real world.
Thanks in Advance....
PS: both java class's are below.... the first is my gui class and the bottom
is my binary tree class.....
############################################################################
#################
BINT CLASS
############################################################################
#################
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
public class BinT extends JFrame {
JTextField input;
JTextArea display;
JScrollPane displayScroller;
JButton go, clear, transverse;
JLabel lab;
JPanel holder, inPan, txtPan, butPan;
BinaryTree myTree = new BinaryTree();
public BinaryTreeGUI() {
SetupWindow();
}
public void SetupWindow() {
setTitle("Binary Tree");
setResizable(false);
setSize(800, 600);
holder = new JPanel(new BorderLayout());
holder.setBorder(new EmptyBorder(5,5,5,5));
//###################### input Panel ######################
inPan = new JPanel();
inPan.setBorder( new EtchedBorder() );
inPan.setPreferredSize(new Dimension(387, 35));
lab = new JLabel("Input Products in Binary Tree : ");
input = new JTextField("");
input.setPreferredSize(new Dimension(70, 20));
go = new JButton("GO");
go.setPreferredSize(new Dimension(51, 20));
go.setToolTipText("Build Binary Tree");
ActionListener act = new ActionListener() {
public void actionPerformed(ActionEvent e) {
//handle errors in input
int treeSize = Integer.parseInt(input.getText()); // get user
input
try {
//check if not equal to ZERO
if (treeSize < 1) {
display.setText("ERROR! Input Must be Greater than ZERO
(Integer)");
}
else {
for (int i = 0; i < treeSize; i++) {
Product prod = new Product();
prod.enterData();
myTree.insert(prod);
//System.out.println("Current Binary Tree :"+myTree);
}
display.setText(""+myTree);
}
}
catch(NumberFormatException er) {
display.setText("ERROR! Input Must be an Integer.");
}
}
};
go.addActionListener(act);
inPan.add(lab);
inPan.add(input);
inPan.add(go);
//###################### button Panel ######################
butPan = new JPanel(new BorderLayout());
butPan.setBorder(new CompoundBorder(new EtchedBorder(),
new EmptyBorder(5,40,5,40)));
butPan.setPreferredSize(new Dimension(387, 35));
clear = new JButton("Clear Tree");
clear.setPreferredSize(new Dimension(130, 20));
clear.setToolTipText("Clear Binary Tree");
act = new ActionListener() {
public void actionPerformed(ActionEvent e) {
myTree.clear();
display.setText("");
}
};
clear.addActionListener(act);
transverse = new JButton("Transverse Tree");
transverse.setPreferredSize(new Dimension(130, 20));
transverse.setToolTipText("Transverse Binary Tree");
act = new ActionListener() {
public void actionPerformed(ActionEvent e) {
//myTree.inOrderTrav();
//Need to change the method from void to String
display.setText("");
}
};
transverse.addActionListener(act);
butPan.add( BorderLayout.WEST, transverse);
butPan.add( BorderLayout.EAST, clear);
holder.add( BorderLayout.WEST,inPan);
holder.add( BorderLayout.EAST,butPan);
getContentPane().add( BorderLayout.NORTH, holder);
//###################### text Panel ######################
txtPan = new JPanel(new BorderLayout());
getContentPane().add( BorderLayout.SOUTH, txtPan );
display = new JTextArea();
//display.setEditable(false);
displayScroller = new JScrollPane(display,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
getContentPane().add(displayScroller);
}
public static void main(String[] args) {
BinT frame = new BinT();
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
############################################################################
#########
BINARY TREE CLASS
############################################################################
#########
public class BinaryTree {
protected TreeNode root;
public BinaryTree() {
root = null;
}
public boolean isEmpty() {
return root == null;
}
public boolean isFull() {
return false;
}
public void clear() {
root = null;
}
public void insert (Product obj) {
root = insert( obj, root );
}
private TreeNode insert(Product obj, TreeNode root) {
if (root == null) { // empty root/tree
root = new TreeNode(obj);
}
else if (obj.code.compareTo(root.prod.code) < 0) {
root.left = insert( obj, root.left );
}
else if (obj.code.compareTo(root.prod.code) > 0) {
root.right = insert( obj, root.right );
}
return(root);
}
public String toString() {
if (isEmpty())
return "";// new String("EMPTY TREE");
else
return (root.toString());
}
public void inOrderTrav(){
System.out.print("\nIn Order Traversal : ");
inOrderTrav(root);
}
protected void inOrderTrav(TreeNode tn){
if(tn == null)
return;
inOrderTrav(tn.left);
System.out.print(tn.prod.code+" ");
inOrderTrav(tn.right);
}
private class TreeNode {
protected Product prod;
protected TreeNode left, right;
private TreeNode(Product obj) {
prod = obj;
left = null;
right = null;
}
public String toString() {
String str = new String();
str += prod.code;
if (left == null)
str += "";
else str +=
" L:"+left.toString();
if (right == null)
str += "";
else str +=
" R:"+right.toString();
return(str);
}
} // end inner TreeNode class
} // end BinaryTree class
I have this assement for college that i have completed in building a "binary
tree" using recursion.
It prints fine through the "System.out.println" command and shows each level
as its built
How can I achieve the same results for my swing textArea as it is displayed
through the above command?
This is not part of the assement but more for my own learning process of
displaying things for the real world.
Thanks in Advance....
PS: both java class's are below.... the first is my gui class and the bottom
is my binary tree class.....
############################################################################
#################
BINT CLASS
############################################################################
#################
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
public class BinT extends JFrame {
JTextField input;
JTextArea display;
JScrollPane displayScroller;
JButton go, clear, transverse;
JLabel lab;
JPanel holder, inPan, txtPan, butPan;
BinaryTree myTree = new BinaryTree();
public BinaryTreeGUI() {
SetupWindow();
}
public void SetupWindow() {
setTitle("Binary Tree");
setResizable(false);
setSize(800, 600);
holder = new JPanel(new BorderLayout());
holder.setBorder(new EmptyBorder(5,5,5,5));
//###################### input Panel ######################
inPan = new JPanel();
inPan.setBorder( new EtchedBorder() );
inPan.setPreferredSize(new Dimension(387, 35));
lab = new JLabel("Input Products in Binary Tree : ");
input = new JTextField("");
input.setPreferredSize(new Dimension(70, 20));
go = new JButton("GO");
go.setPreferredSize(new Dimension(51, 20));
go.setToolTipText("Build Binary Tree");
ActionListener act = new ActionListener() {
public void actionPerformed(ActionEvent e) {
//handle errors in input
int treeSize = Integer.parseInt(input.getText()); // get user
input
try {
//check if not equal to ZERO
if (treeSize < 1) {
display.setText("ERROR! Input Must be Greater than ZERO
(Integer)");
}
else {
for (int i = 0; i < treeSize; i++) {
Product prod = new Product();
prod.enterData();
myTree.insert(prod);
//System.out.println("Current Binary Tree :"+myTree);
}
display.setText(""+myTree);
}
}
catch(NumberFormatException er) {
display.setText("ERROR! Input Must be an Integer.");
}
}
};
go.addActionListener(act);
inPan.add(lab);
inPan.add(input);
inPan.add(go);
//###################### button Panel ######################
butPan = new JPanel(new BorderLayout());
butPan.setBorder(new CompoundBorder(new EtchedBorder(),
new EmptyBorder(5,40,5,40)));
butPan.setPreferredSize(new Dimension(387, 35));
clear = new JButton("Clear Tree");
clear.setPreferredSize(new Dimension(130, 20));
clear.setToolTipText("Clear Binary Tree");
act = new ActionListener() {
public void actionPerformed(ActionEvent e) {
myTree.clear();
display.setText("");
}
};
clear.addActionListener(act);
transverse = new JButton("Transverse Tree");
transverse.setPreferredSize(new Dimension(130, 20));
transverse.setToolTipText("Transverse Binary Tree");
act = new ActionListener() {
public void actionPerformed(ActionEvent e) {
//myTree.inOrderTrav();
//Need to change the method from void to String
display.setText("");
}
};
transverse.addActionListener(act);
butPan.add( BorderLayout.WEST, transverse);
butPan.add( BorderLayout.EAST, clear);
holder.add( BorderLayout.WEST,inPan);
holder.add( BorderLayout.EAST,butPan);
getContentPane().add( BorderLayout.NORTH, holder);
//###################### text Panel ######################
txtPan = new JPanel(new BorderLayout());
getContentPane().add( BorderLayout.SOUTH, txtPan );
display = new JTextArea();
//display.setEditable(false);
displayScroller = new JScrollPane(display,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
getContentPane().add(displayScroller);
}
public static void main(String[] args) {
BinT frame = new BinT();
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
############################################################################
#########
BINARY TREE CLASS
############################################################################
#########
public class BinaryTree {
protected TreeNode root;
public BinaryTree() {
root = null;
}
public boolean isEmpty() {
return root == null;
}
public boolean isFull() {
return false;
}
public void clear() {
root = null;
}
public void insert (Product obj) {
root = insert( obj, root );
}
private TreeNode insert(Product obj, TreeNode root) {
if (root == null) { // empty root/tree
root = new TreeNode(obj);
}
else if (obj.code.compareTo(root.prod.code) < 0) {
root.left = insert( obj, root.left );
}
else if (obj.code.compareTo(root.prod.code) > 0) {
root.right = insert( obj, root.right );
}
return(root);
}
public String toString() {
if (isEmpty())
return "";// new String("EMPTY TREE");
else
return (root.toString());
}
public void inOrderTrav(){
System.out.print("\nIn Order Traversal : ");
inOrderTrav(root);
}
protected void inOrderTrav(TreeNode tn){
if(tn == null)
return;
inOrderTrav(tn.left);
System.out.print(tn.prod.code+" ");
inOrderTrav(tn.right);
}
private class TreeNode {
protected Product prod;
protected TreeNode left, right;
private TreeNode(Product obj) {
prod = obj;
left = null;
right = null;
}
public String toString() {
String str = new String();
str += prod.code;
if (left == null)
str += "";
else str +=
" L:"+left.toString();
if (right == null)
str += "";
else str +=
" R:"+right.toString();
return(str);
}
} // end inner TreeNode class
} // end BinaryTree class