Question: I have a PhoneBookDriver class that utilizes a GUI that accepts a name, address, and phone number. The code gathers these entries and uses them

I have a PhoneBookDriver class that utilizes a GUI that accepts a name, address, and phone number. The code gathers these entries and uses them to create a Person object and appends the object to a Binary Search Tree. The "Add" button works as intended, but I am unable to remove an object with the "Remove" button once it has been appended to the BST. Please help! PhoneBookDriver.java package lab3; /* * Name: Michael Barry * Date: 1/27/2023 * Class: CS302 * * Info: Phone Book Driver class to represent address book. */ //Imports import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.Serializable; import java.util.Iterator; public class PhoneBookDriver implements Serializable { //GUI Parameters private JPanel mainPanel; private JPanel entryPanel; private JTextField textName; private JPanel buttonPanel; private JButton addButton; private JTextArea output; private JTextField textPhone; private JTextField textAddress; private JLabel title; private JLabel labelPhone; private JLabel labelAddress; private JButton removeButton; private JButton searchButton; private JLabel labelName; private JButton displayButton; //GUI Functionality public PhoneBookDriver() { //Initialize BST BinarySearchTree searchTree = new BinarySearchTree<>(); //"ADD" Button addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //new person object for each button press Person p = new Person(); //gather user input p.setName(textName.getText()); p.setAddress(textAddress.getText()); p.setPhoneNum(textPhone.getText()); searchTree.add(p.toString()); Iterator itr = searchTree.iterator(); //clear previous text output.setText(""); //print to GUI text field while (itr.hasNext()) { output.append(itr.next()); //Console Test //System.out.println(itr.next()); } } }); //"REMOVE" Button (TESTING) removeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String name = textName.getText(); output.setText(""); if (searchTree.contains(name)) { searchTree.remove(name); } Iterator itr = searchTree.iterator(); while (itr.hasNext()) { output.append(itr.next()); } } }); //"SEARCH" Button searchButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String str = textName.getText(); if (str.isEmpty()) { str = textAddress.getText(); } if (str.isEmpty()) { str = textPhone.getText(); } output.setText(searchTree.getEntry(str).toString()); output.setText(str); } }); //"DISPLAY" Button (To revert back to main list after search) displayButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Iterator itr = searchTree.iterator(); //clear previous text output.setText(""); while (itr.hasNext()) { //print to GUI text field output.append(itr.next()); //Console Test //System.out.println(itr.next()); } } }); } //Main Method to support GUI integration public static void main(String[] args){ JFrame frame = new JFrame("PhoneBook Application"); frame.setContentPane(new PhoneBookDriver().mainPanel); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!