Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I am supposed to create a phonebook GUI within Java, where a user enters a name, address, and phone number and is added to an
I am supposed to create a phonebook GUI within Java, where a user enters a name, address, and phone number and is added to an ordered list (via binary search tree) and displayed within the GUI. What I need help with specifically is creating a new Person() object every time the user enters text and clicks the "Add" button. Below is by driver code.
*The p variable is supposed to be a person object ( i.e. searchTree.add(p.toString()) ).
package lab3; 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 { private JPanel mainPanel; private JPanel entryPanel; private JTextField textName; private JLabel labelName; private JPanel buttonPanel; private JButton addButton; private JButton Display; private JTextArea output; private JTextField textPhone; private JTextField textAddress; private JLabel title; private JLabel labelPhone; private JLabel labelAddress; private JButton removeButton; private JButton searchButton; public PhoneBookDriver() { //Fields BinarySearchTreesearchTree = new BinarySearchTree<>(); Iterator itr = searchTree.iterator(); addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { searchTree.add(p.toString()); } }); removeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (searchTree.isEmpty()) return; else searchTree.remove(p.toString()); } }); searchButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String str; if (textName.getText() != null){ str = textName.getText(); } if (textAddress.getText() != null){ str = textAddress.getText(); } if (textPhone.getText() != null){ str = textPhone.getText(); } searchTree.getEntry(str); } }); Display.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { while(itr.hasNext()) { output.setText(output.getText() + " " + itr.next()); } } }); } 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started