How to save an updated JList to a text file after the GUI closes?
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
Code Java:
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 list:(where I am lost)
Code Java:
public saveFile() {
if WindowClosing is pressed{
UpdateContact file=new UpdateContact file();
}
my code where the GUI closes.
Code Java:
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.
Re: How to save an updated JList to a text file after the GUI closes?
can you explain what the problem is?
Are you able to write to the file?
Can you get the data that you want to write to the file?
Can you execute the code to write the file when you want to do the wrigint?
Re: How to save an updated JList to a text file after the GUI closes?
Quote:
Originally Posted by
Norm
can you explain what the problem is?
Are you able to write to the file?
Can you get the data that you want to write to the file?
Can you execute the code to write the file when you want to do the wrigint?
I have a GUI where you can edit and remove contacts. Once you exit the GUI I want the contacts to update in the next file. I have a text file created, but it's not updating the contacts.
Re: How to save an updated JList to a text file after the GUI closes?
Quote:
it's not updating the contacts.
Can you explain what the code does or does not do?
Where is the problem with "updating the contacts"?
I listed several possibilities. Are any of them the problem? Or what is the problem?
Re: How to save an updated JList to a text file after the GUI closes?
Contacts is a list created with a jlist. It is in a dialog box that opens in an email program. The contacts are added to a text file once the user edits the contacts with a jlist. The problem is that it loads the contact information from a text file contacts.txt but it does not add or remove contacts when the user changes it in the jlist GUI.
Re: How to save an updated JList to a text file after the GUI closes?
Quote:
it does not add or remove contacts when the user changes it in the jlist GUI.
I still don't understand about when and where the changes are supposed to be made.
Where in the code are these changes supposed to be made?
Are the changes to be made when the GUI window closes or when?
Does the code try to make the changes and has a problem making the changes?
or is the code completely missing that is supposed to make the changes?
This is a confusing part:
Quote:
contacts are added to a text file once the user edits the contacts
That looks like the code is doing everything that is needed. What more needs to be done if the changes are being written to the text file?
Re: How to save an updated JList to a text file after the GUI closes?
I'm sorry if I am not being clear. There is a contact list GUI that reads the name and emails from a text file. It loads the contacts from the file. But when te user presses add or remove contact, it does not change the contact list in the text file. I want it to say something like if remove is pressed remove certain contact. If save is pressed add new contact to text file.
Re: How to save an updated JList to a text file after the GUI closes?
Quote:
it does not change the contact list in the text file
Is it supposed to change the file?
Is there code to make changes to the file?
If there is code, is there a problem executing the code to make the changes to the file?
Which is the problem?
The current code doesn't work correctly when trying to write to a file
There is no code to write to a file
Re: How to save an updated JList to a text file after the GUI closes?
Thank you for your help. I figured it out.