Question: I am making a GUI where the user can enter a name, address, and phone number and append it to a BST and displayed as
I am making a GUI where the user can enter a name, address, and phone number and append it to a BST and displayed as a list within the GUI. The code that I have is working, but I am supposed to implement serialization and deserialization, so the data can be saved to a file and loaded when the application is run again. I am not sure how to do this, so can I please have some help? Below is my code.
PhoneBookDriver.java
package lab3; //Imports import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; 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; private JButton modifyButton; //GUI Functionality public PhoneBookDriver() { //Initialize BST BinarySearchTree searchTree = new BinarySearchTree<>(); //"ADD" Button addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //new peron object with user input Person p = new Person(textName.getText(), textAddress.getText(), textPhone.getText()); //add Person object searchTree.add(p); Iterator itr = searchTree.iterator(); //clear previous text output.setText(""); //print to GUI text field while (itr.hasNext()) { Person temp = itr.next(); output.append(temp.toString()); } } }); //"REMOVE" Button removeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String name = textName.getText(); Iterator itr = searchTree.iterator(); //set text field to null output.setText(""); //iterate through tree until removable is found while (itr.hasNext()){ Person temp = itr.next(); if (name.equals(temp.getName())){ searchTree.remove(temp); } //append tree without removable else output.append(temp.toString()); } } }); //"SEARCH" Button searchButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //if field is null, search for next field String str = textName.getText(); if (str == null){ str = textAddress.getText(); } if (str == null){ str = textPhone.getText(); } Iterator itr = searchTree.iterator(); output.setText("Search Not Found"); while (itr.hasNext()){ Person temp = itr.next(); if (str.equals(temp.getName())){ output.setText(temp.toString()); } if (str.equals(temp.getAddress())){ output.setText(temp.toString()); } if (str.equals(temp.getPhoneNum())){ output.setText(temp.toString()); } } } }); //"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()) { Person temp = itr.next(); output.append(temp.toString()); } } }); //"MODIFY" Button //deletes entry with same name and appends updated object modifyButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //new peron object with user input Person p = new Person(textName.getText(), textAddress.getText(), textPhone.getText()); String name = textName.getText(); Iterator itr = searchTree.iterator(); output.setText(""); while (itr.hasNext()){ Person temp = itr.next(); if (name.equals(temp.getName())){ searchTree.remove(temp); searchTree.add(p); output.append("NEWLY MODIFIED : " + p.toString()); } else continue; } } }); } //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
Get step-by-step solutions from verified subject matter experts
