I'm having a problem with adding the name, dob month, dob day, and dob year to a binary search tree from the information from the GUI. This has to happen after the "Add Person" button is clicked. I should add it inside the brackets of the
"if(e.getSource() == addPersonButton)" statement, after the "try/catch" statements.
I have a Node.java and Person.java, but I'm not adding them for know. I'll add them later if need to.
My GUI source code:
import javax.naming.NamingException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AddPersonPanel {
//instance variables
private JLabel nameLabel;
private JLabel dobMonthLabel;
private JLabel dobDayLabel;
private JLabel dobYearLabel;
private JComboBox monthsBox;
private JComboBox daysBox;
private JTextField nameField;
private JTextField yearField;
private JButton addPersonButton;
private JButton showTreeButton;
private String[] months = {"January", "February", "March", "April",
"May", "June", "July", "August", "September", "October",
"November", "December"};
private String[] nums = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21",
"22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};
private GridLayout grid;
private JFrame frame;
//constructor
public AddPersonPanel(){
makeComponents();
layoutComponents();
}
private void layoutComponents(){
Container container = frame.getContentPane();
grid = new GridLayout(3, 1);
container.setLayout(grid);
container.add(namePanel());
container.add(dobPanel());
container.add(buttonPanel());
frame.setSize(500, 200);
frame.setLocation(400, 300);
}
private JPanel namePanel(){
JPanel container = new JPanel(new FlowLayout());
container.add(nameLabel);
container.add(nameField);
return container;
}
private JPanel dobPanel(){
JPanel container = new JPanel(new FlowLayout());
container.add(dobMonthLabel);
container.add(monthsBox);
container.add(dobDayLabel);
container.add(daysBox);
container.add(dobYearLabel);
container.add(yearField);
return container;
}
private JPanel buttonPanel() {
JPanel container = new JPanel(new FlowLayout());
container.add(addPersonButton);
container.add(showTreeButton);
return container;
}
public void setVisible(boolean vis){
frame.setVisible(vis);
}
private void makeComponents(){
frame = new JFrame("Add Person");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
nameLabel = new JLabel("Name: ");
dobMonthLabel = new JLabel("DOB Month: ");
dobDayLabel = new JLabel("DOB Day: ");
dobYearLabel = new JLabel("DOB Year: ");
monthsBox = new JComboBox(months);
daysBox = new JComboBox(nums);
nameField = new JTextField(15);
yearField = new JTextField(8);
addPersonButton = new JButton("Add Person");
addPersonButton.addActionListener(new Handler());
showTreeButton = new JButton("Show Tree");
showTreeButton.addActionListener(new Tree());
}
public static void main(String[] args){
AddPersonPanel e = new AddPersonPanel();
e.setVisible(true);
}
public class Handler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == addPersonButton)
{
try {
String n = nameField.getText();
if(n.equals(""))
throw new NamingException();
}
catch (NamingException ae) {
JOptionPane.showMessageDialog(frame, "Name may not be blank", "Name error", JOptionPane.ERROR_MESSAGE);
}
try {
int y = Integer.parseInt(yearField.getText());
if((y < 0) && (y > 9999))
throw new NumberFormatException();
}
catch (NumberFormatException ea) {
JOptionPane.showMessageDialog( frame, "The year must be > 0 and < 9999", "Year Error", JOptionPane.ERROR_MESSAGE);
}
try
{
if(monthsBox.getSelectedItem() == null)
throw new IndexOutOfBoundsException();
}
catch(IndexOutOfBoundsException ae) {
JOptionPane.showMessageDialog( frame, "DOB Day", "Day must be > 0 and < 31", JOptionPane.ERROR_MESSAGE);
}
}
}
}
public class Tree implements ActionListener {
public void actionPerformed(ActionEvent e) {
}
}
}
This is my binary search tree code (I can't modify it):
import java.util.ArrayList;
//You MAY NOT MODIFY this class
public class BinarySearchTree
{
private Node root;
/**
Constructs an empty tree.
*/
public BinarySearchTree()
{
root = null;
}
/**
Returns the smallest object in this tree
@return the smallest object or null if the tree is empty
*/
public Comparable smallest()
{
if (root == null) return null;
else return root.smallest();
}
/**
Inserts a new node into the tree.
@param obj the object to insert
*/
public void add(Comparable obj)
{
Node newNode = new Node();
newNode.data = obj;
newNode.left = null;
newNode.right = null;
if (root == null) root = newNode;
else root.addNode(newNode);
}
/**
Tries to find an object in the tree.
@param obj the object to find
@return true if the object is contained in the tree
*/
public boolean find(Comparable obj)
{
Node current = root;
while (current != null)
{
int d = current.data.compareTo(obj);
if (d == 0) return true;
else if (d > 0) current = current.left;
else current = current.right;
}
return false;
}
/**
Tries to remove an object from the tree. Does nothing
if the object is not contained in the tree.
@param obj the object to remove
*/
public void remove(Comparable obj)
{
// Find node to be removed
Node toBeRemoved = root;
Node parent = null;
boolean found = false;
while (!found && toBeRemoved != null)
{
int d = toBeRemoved.data.compareTo(obj);
if (d == 0) found = true;
else
{
parent = toBeRemoved;
if (d > 0) toBeRemoved = toBeRemoved.left;
else toBeRemoved = toBeRemoved.right;
}
}
if (!found) return;
// toBeRemoved contains obj
// If one of the children is empty, use the other
if (toBeRemoved.left == null || toBeRemoved.right == null)
{
Node newChild;
if (toBeRemoved.left == null)
newChild = toBeRemoved.right;
else
newChild = toBeRemoved.left;
if (parent == null) // Found in root
root = newChild;
else if (parent.left == toBeRemoved)
parent.left = newChild;
else
parent.right = newChild;
return;
}
// Neither subtree is empty
// Find smallest element of the right subtree
Node smallestParent = toBeRemoved;
Node smallest = toBeRemoved.right;
while (smallest.left != null)
{
smallestParent = smallest;
smallest = smallest.left;
}
// smallest contains smallest child in right subtree
// Move contents, unlink child
toBeRemoved.data = smallest.data;
smallestParent.left = smallest.right;
}
/**
Prints the contents of the tree in sorted order.
*/
public void print()
{
ArrayList<Boolean> lines = new ArrayList<Boolean>();
if (root != null)
root.printNodes(lines);
}
/**
* Prints the contents of the tree in sorted order.
**/
public String printInOrder()
{
String t = "";
if (root != null)
t = root.printNodesInOrder(t);
System.out.println();
return t;
}
/**
* @return the root
*/
public Node getRoot()
{
return root;
}
/**
* @param root the root to set
*/
public void setRoot(Node root)
{
this.root = root;
}
}