I am writing code for a simple email sender with a contact list GUI (using JList) that pops up when you click to TO: button in the email GUI. The contact list GUI allows the user to select a contact to send an email to, and it also allows the user to delete or add contacts to the list. I cannot figure out how to update the contacts.txt list after the person is done updating the contacts. I want the text file to update after the user exits the contact list GUI.
my code for adding the contacts.txt file
public void setUpContactList() { //sets up the contact list from the contacts.txt file addressBook = new ContactList(); try { Scanner scanFile = new Scanner(new File("contacts.txt")); while(scanFile.hasNext()) { String contactInformation = scanFile.nextLine(); //split by contact Scanner contactScanner = new Scanner(contactInformation); String info = contactScanner.next(); String first = ""; String last = ""; String email = ""; if(info.contains("@")) { email = info; } else { first = info; last = contactScanner.next(); email = contactScanner.next(); } Contact contactA = new Contact(first, last, email); addressBook.add(contactA); } } catch(FileNotFoundException e) {}
my code to update the listwhere I am lost)
public saveFile() { if WindowClosing is pressed{ UpdateContact file=new UpdateContact file(); }
my code where the GUI closes.
public void windowClosing(WindowEvent arg0) { toField.setText(toField.getText().concat(clUI.getRecipients())); saveFile(); }
Sorry if I formatted this question wrong, this is the first time I have used stackoverflow. Any help is appreciated.