Answered step by step
Verified Expert Solution
Question
1 Approved Answer
-?Java GUI phonebook help?????? Hello im having trouble with this GUI for java. I keep getting a java.io.FileNotFoundException: phonebook.txt (The system cannot find the file
-?Java GUI phonebook help??????
Hello im having trouble with this GUI for java. I keep getting a java.io.FileNotFoundException: phonebook.txt (The system cannot find the file specified)? error and I dont know how to fix it
?heres the code for the Phone book and GUI phone books
import java.awt.EventQueue; import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Window.Type; public class GUIphonebook { public static JFrame frmPhonebook; public static JTextField txtName; public static JTextField txtNumber; public static JTextField txtNotes; public static JTextPane txtAll; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { GUIphonebook window = new GUIphonebook(); window.frmPhonebook.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public GUIphonebook() throws Exception { initialize(); } private void initialize() throws Exception { Phone_Book.readPhone_Book("phonebook.txt"); Phone_Book.sortList(); frmPhonebook = new JFrame(); frmPhonebook.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 12)); frmPhonebook.setType(Type.UTILITY); frmPhonebook.setTitle("PHONEBOOK"); frmPhonebook.setBounds(100, 100, 461, 503); frmPhonebook.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel mainP = new JPanel(); mainP.setBackground(Color.GRAY); frmPhonebook.getContentPane().add(mainP, BorderLayout.CENTER); mainP.setLayout(null); JButton btnNewButton = new JButton("Find Contact"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Phone_Book.displayContact(); } }); btnNewButton.setBounds(-6, 5, 130, 29); btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 14)); mainP.add(btnNewButton); JButton btnAddContact = new JButton("Add Contact"); btnAddContact.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Phone_Book.addContact(); txtName.setText("Name"); txtNumber.setText("Number"); txtNotes.setText("Notes"); Phone_Book.sortList(); try { Phone_Book.CopyPhoneBookToFile("Phonebook.txt"); } catch (Exception e) { e.printStackTrace(); } } }); btnAddContact.setFont(new Font("Tahoma", Font.BOLD, 14)); btnAddContact.setBounds(139, 5, 140, 29); mainP.add(btnAddContact); JButton btnListContacts = new JButton("List Contacts"); btnListContacts.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Phone_Book.listAllContacts(); } }); btnListContacts.setFont(new Font("Tahoma", Font.BOLD, 14)); btnListContacts.setBounds(284, 5, 140, 29); mainP.add(btnListContacts); txtName = new JTextField(); txtName.setFont(UIManager.getFont("Button.font")); txtName.setBounds(139, 81, 146, 26); txtName.setText("Name"); mainP.add(txtName); txtName.setColumns(10); txtNumber = new JTextField(); txtNumber.setFont(UIManager.getFont("Button.font")); txtNumber.setBounds(139, 141, 146, 26); txtNumber.setText("Number"); mainP.add(txtNumber); txtNumber.setColumns(10); txtNotes = new JTextField(); txtNotes.setFont(UIManager.getFont("Button.font")); txtNotes.setBounds(139, 193, 146, 26); txtNotes.setText("Notes"); mainP.add(txtNotes); txtNotes.setColumns(10); txtAll = new JTextPane(); final JScrollPane Window = new JScrollPane(txtAll); Window.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); Window.setLocation(101, 260); Window.setSize(221, 129); mainP.add(Window); } }
-------------------------------------------------------------------------------------------------------------------------------------------
import java.io.*; import java.util.*; class Entry { public String name, number, notes; } public class Phone_Book { public static Entry[] contactList = new Entry[200]; public static int num_entries; public static Scanner stdin = new Scanner(System.in); public static Object entryList; public static void readPhone_Book(String Phonebook) throws Exception { File phonebook; phonebook = new File(Phonebook); Scanner S = new Scanner(phonebook); while (S.hasNext()) { contactList[num_entries] = new Entry(); contactList[num_entries].name = S.nextLine(); contactList[num_entries].number = S.nextLine(); contactList[num_entries].notes = S.nextLine(); num_entries++; } S.close(); } public static void addContact() { String name = GUIphonebook.txtName.getText(); String number = GUIphonebook.txtNumber.getText(); String notes = GUIphonebook.txtNotes.getText(); contactList[num_entries] = new Entry(); contactList[num_entries].name = name; contactList[num_entries].number = number; contactList[num_entries].notes = notes; num_entries++; } public static int find(String Name) { for (int i = 0; i num_entries; i++) { if (contactList[i].name.equalsIgnoreCase(Name)) return i; } return -1; } public static void displayContact() { String name = GUIphonebook.txtName.getText(); int x = find(name); String name1 = contactList[x].name; String number = contactList[x].number; String notes = contactList[x].notes; GUIphonebook.txtName.setText(name1); GUIphonebook.txtNumber.setText(number); GUIphonebook.txtNotes.setText(notes); } public static void listAllContacts() { String all = ""; int i = 0; while (i num_entries) { all = all + (contactList[i].name + " " + contactList[i].number + " " + contactList[i].notes + " " + " "); i++; } GUIphonebook.txtAll.setText(all); } public static void sortList() { int i; Entry temp; temp = new Entry(); for (int j = 0; j num_entries; j++) { for (i = j + 1; i num_entries; i++) { if (contactList[j].name.compareToIgnoreCase(contactList[i].name) > 0) { temp = contactList[j]; contactList[j] = contactList[i]; contactList[i] = temp; } } } } public static void CopyPhoneBookToFile(String FileName) throws Exception { FileOutputStream out = new FileOutputStream(FileName); PrintStream P = new PrintStream(out); for (int i = 0; i num_entries; i++) { P.println(contactList[i].name); P.println(contactList[i].number); P.println(contactList[i].notes); } } }
D Project *?1?? ? Phone-Bookjava- Phone-Bookiml x G GUlphonebook.java while (1 0) temp - contactiist[ contactList [j] = contectList [i] contactList[i] temp static void CopyEhoneBookToFile(String Filelane) chrowS Exception [ FileOutputStream out new FileOutputStrea?(Eilelene); PrintStream P-new PrintStre?(out) for (int 1-0; 1 ?Scanner. 1ava:611) at Phone Book.readPhone_Book(Phone Book.1ava: 14) at GUIphonebook.nitiallze?GUI at GUIphonebook.
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