Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: How to save an updated JList to a text file after the GUI closes?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

     
        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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to save an updated JList to a text file after the GUI closes?

    Quote Originally Posted by Norm View Post
    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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: How to save an updated JList to a text file after the GUI closes?

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: How to save an updated JList to a text file after the GUI closes?

    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:
    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?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: How to save an updated JList to a text file after the GUI closes?

    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
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to save an updated JList to a text file after the GUI closes?

    Thank you for your help. I figured it out.

Similar Threads

  1. Save array to text file
    By JGW in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 6th, 2013, 10:45 AM
  2. Saving login information into a text file in my GUI App
    By lf2killer in forum Java Theory & Questions
    Replies: 5
    Last Post: November 16th, 2012, 09:14 AM
  3. File Text displays jumbled in JList
    By gammaman in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: April 27th, 2012, 02:59 PM
  4. Replies: 0
    Last Post: December 15th, 2011, 01:14 PM

Tags for this Thread