Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below is a code that accepts user input within a GUI, stores the input in a BST, then displays the Person objects within a text

Below is a code that accepts user input within a GUI, stores the input in a BST, then displays the Person objects within a text field within the GUI. All buttons, but the "save" and "load" buttons work as intended. Please help me with the serialization and deserialization within my code (located at bottom of code).

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; private JButton saveButton; private JButton loadButton; //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; } } }); //"LOAD" Button saveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { save(searchTree, "person.txt"); } }); //"SAVE" Button loadButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { load("person.txt"); } }); } static void save(BinarySearchTree data, String fileName) { try (FileOutputStream fileOut = new FileOutputStream(fileName); ObjectOutputStream out = new ObjectOutputStream(fileOut)) { out.writeObject(data); } catch (Exception e) { e.printStackTrace(); } } static BinarySearchTree load(String fileName) { try (FileInputStream fileIn = new FileInputStream(fileName); ObjectInputStream in = new ObjectInputStream(fileIn)) { return (BinarySearchTree) in.readObject(); } catch (Exception e) { e.printStackTrace(); } return null; } //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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatio Temporal Database Management International Workshop Stdbm 99 Edinburgh Scotland September 10 11 1999 Proceedings Lncs 1678

Authors: Michael H. Bohlen ,Christian S. Jensen ,Michel O. Scholl

1999th Edition

3540664017, 978-3540664017

More Books

Students also viewed these Databases questions

Question

Define response or interview bias.

Answered: 1 week ago