-1

I'm using JavaFX.

When a certain file doesn't load, I want the stage to close and the code in that instance to stop executing.

catch (FileNotFoundException ex)
{
    stage.close();
    System.out.println("This should not appear.");
}

Calling stage.close() closes the program, but before it does, some of the other code in the program gets executed. I don't want that to happen.

How can I close the current stage immediately (or at least prevent other code from executing)?

Pikamander2
  • 7,332
  • 3
  • 48
  • 69
  • 1
    Possible duplicate of [How to close an entire JavaFX application?](http://stackoverflow.com/questions/12153622/how-to-close-an-entire-javafx-application) – limonik Nov 30 '16 at 08:13
  • @limonik - I don't want to close the entire application. I only want to close the current stage. – Pikamander2 Nov 30 '16 at 08:15
  • You said: "I want the stage to close and the program to stop executing.". So what do you really want to achieve? And what's that "some of other code" that get executed? – DVarga Nov 30 '16 at 08:18
  • @DVarga- I may not be using the right terminology, but basically, my program starts out with one stage. That stage spawns another stage. I want the second stage to exit without affecting the first stage, and I want it to exit without continuing to call more code. – Pikamander2 Nov 30 '16 at 08:21

1 Answers1

0

Use stage.setOnCloseRequest to perform actions before your stage closes. Like

 public void start(Stage primaryStage)
 {
       // do all the regular stuff.
       primaryStage.setOnCloseRequest(E -> {
            // perform actions before closing
       });
 }

Overriding the stop() method in your Application class is also a valid method to add a shutdown hook. Though from experiance it isn't always called when your Application shuts down.

Lastly if you want to shutdown your entire Application, please use Platform.exit() (static method). The stage.close() method is equal to stage.hide(), meaning only the window will be hidden, the Application will not shutdown.

EDIT1:

If you want to execute code when your stage is hiding. Use the following snippit.

 primaryStage.setOnHiding(E -> {
      // perform actions upon hiding.
 });

EDIT2:

If you want a first Stage to wait for a second Stage, and continu after the second Stage is closed use secondaryStage.showAndWait(). This way you just close the secondary Stage if you want to continu with the execution on the first Stage.

n247s
  • 1,898
  • 1
  • 12
  • 30
  • I've tried Platform.exit, but it closes more than just the current stage. It also closes the other stages as well. It also doesn't prevent the code after it. – Pikamander2 Nov 30 '16 at 08:32
  • The way that I'm creating the second stage is with `Application game = `gameType.newInstance(); Stage newStage = new Stage(); game.start(newStage);` I don't think that showAndWait would work there, since the game object is creating its own stage. – Pikamander2 Nov 30 '16 at 08:39
  • You create 2 apllication class instances? Thats a bad idea. Use only one, and for the second stage do `newStage.showAndWait()`. It will give the results you need. – n247s Nov 30 '16 at 08:47
  • 1
    Maybe I'm missing something here, but if you always want the stage to close when `FileNotFoundException` is thrown, why would you put some code inside the `catch` block that you never want to execute? Put all the code you don't want execute if the stage is closed inside the `try` block, and then the `stage.close()` statement in the `catch` block. This way the code in the `try` block will only get executed if no exception is thrown. – Jonatan Stenbacka Nov 30 '16 at 09:02