1 Reply Latest reply: Feb 6, 2013 2:51 AM by jugen RSS

    Pausing Application Close

    jugen
      My application consists of a number of views and editors.

      When the user closes the application it checks to see if any editors have any unsaved changes and if so prompts the user for each dirty editor, asking if it should save that editors changes.

      This process happens in a Window Event Handler registered with the primary stage's setOnCloseRequest() method.

      A problem arose when switching from one editor to the next in order to show and prompt the user because the switching animation requires a separate thread. What would happen is that as soon as the thread was invoked the handle() method would "complete" and the JVM would shut down.

      So what I needed was a way to prevent the handle() method from exiting before all the dirty editors had been processed, but that wouldn't hog the FX Application Thread and lock up the GUI.

      The solution I came up with was to create a new transparent stage with showAndWait(), like so:
      private Stage holdWindowClose;
      .
      .
         holdWindowClose = new Stage( StageStyle.TRANSPARENT );
         holdWindowClose.showAndWait();
      This prevented the handle method from completing. Then when all the dirty editors had been processed holdWindowClose is hidden / closed, which then allows the JVM to continue shutting down.

      My question then is there a better way of doing this or is this method acceptable ?
      Phrased another way would be: Is there a more proper way to do this ?

      Thanks.
        • 1. Re: Pausing Application Close
          jugen
          Another way I tried and that works was to consume the close request event and then to use System.exit() once any dirty editors had been processed.

          Which would be the better way of doing it then ?

          1) showAndWait() and then hide(), or
          2) consume() and then exit()

          Thanks.