Results 1 to 14 of 14
  1. #1
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default save on exit doesn't work

    Below is the entire main function for my application. The open command correctly calls the function that opens the data file. However, the save command is never called. I had expected that when the user presses the x button to terminate the application the invokeLater block would close and return focus to line 14.

    Java Code:
    public static void main(String[] args) throws IOException {
    Malt tempmalt=new Malt();
        tempmalt.open();
       
                 javax.swing.SwingUtilities.invokeLater(new Runnable() {
                 @Override
                 public void run() { 
                //beer_main_panel frame = new beer_main_panel(); 
                CreateAndShowGUI();
             }
        }); 
        
        //remember to add calls to save and close class data files
        tempmalt.save();
        }
    Last edited by ras_oscar; 10-19-2013 at 04:20 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: save on exit doesn't work

    Add some println() statements that print out messages as the code is executed.
    I suspect that the methods are called in this order:
    open
    save
    CreateAndShowGUI
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: save on exit doesn't work

    I ammended my main function as follows;

    Java Code:
    public static void main(String[] args) throws IOException {
      
        Malt tempmalt=new Malt();
        System.out.println("This is One");
        tempmalt.open();
        System.out.println("This is two");
                 javax.swing.SwingUtilities.invokeLater(new Runnable() {
                 @Override
                 public void run() { 
                 CreateAndShowGUI();
             }
        }); 
        
           System.out.println("This is three");
        tempmalt.save();
        System.out.println("This is Four");
        System.in.read();
        
        
        }
    I got "this is one" and " this is two"
    Three and four were never executed.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: save on exit doesn't work

    Please make a small simpler program that shows the problem when executed.
    When I exeucute the following:
    Java Code:
           System.out.println("This is two");
           javax.swing.SwingUtilities.invokeLater(new Runnable() {
              @Override
              public void run() { 
                  System.out.println("This is running");
              }
            }); 
         
           System.out.println("This is three");
    I get this:
    This is two
    This is three
    This is running
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: save on exit doesn't work

    I had a run time error related to the tempmalt.open() call, that was halting the process before getting to three and four. I have corrected the error, and now you are quite correct. the Statements "this is one" "this is two" "this is three" and "this is four" are printed before the GUI is displayed. Where do I insert the cleanup code to save the open databases before quitting? I will see if I can gin up a small example that recreates the problem in 40 lines or less.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: save on exit doesn't work

    Where do I insert the cleanup code to save the open databases before quitting?
    In the Malt class. The main() method starts the class and exits leaving the Malt object running.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: save on exit doesn't work

    So if I run the program three times there's three copies of the class objects reminig in memory? I thought Java did automatic cleanup and closure of objects as they went out of scope, meaning the class instances would go out of scope when main() closes. I guess i have some more reading to do.

  8. #8
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,093
    Rep Power
    6

    Default Re: save on exit doesn't work

    Judging by this thread, you should investigate threading, concurrent execution and the swing Event Dispatch Thread (EDT).
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: save on exit doesn't work

    Quote Originally Posted by Norm View Post
    In the Malt class. The main() method starts the class and exits leaving the Malt object running.
    The malt class already has a save() function. I'm looking for the proper place to insert a call to the function.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: save on exit doesn't work

    Look at the Thread class's doc especially about daemon threads.

    How does the program finish executing? Does it call the System exit() method?
    Call any end of processing methods like save() before exiting the program.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: save on exit doesn't work

    Gimbal2, Norm;

    Thanks for the recommendations re additional reading. I had never heard the term Daemon thread before today, but it makes sense. Norm, to answer your question above, the application closes when the user presses the "X" button in the upper right corner of the display panel. I will read further on the subjects listed above and see where I can slip in the save() call.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: save on exit doesn't work

    the application closes when the user presses the "X" button
    The program needs to catch that user action(pressing "X") in a listener so it can do the closing needed to be done before the program exits.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: save on exit doesn't work

    The Runtime class has a way of adding a shutdown hook which is executed when the app shuts down.
    I would argue, since 1.3 (I think) that that's the place to stick "tidy up" code, rather than in the GUI.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: save on exit doesn't work

    Shutdownhook solved the problem. All is well. Thank You all for pointing me in a successful direction, and being patient.

Similar Threads

  1. Replies: 4
    Last Post: 04-02-2013, 05:01 PM
  2. I wonder why this doesn't work...
    By louboulos in forum New To Java
    Replies: 3
    Last Post: 05-13-2012, 04:59 PM
  3. Jar doesn't work
    By mad72584 in forum New To Java
    Replies: 35
    Last Post: 08-07-2011, 05:22 PM
  4. Replies: 5
    Last Post: 02-11-2011, 08:33 PM
  5. why this doesn't work?
    By hitesh_public in forum New To Java
    Replies: 5
    Last Post: 08-09-2010, 08:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •