title: Call JavaFX in Java program and wait for wait to exit before running more code
body: <p>In my java program i give some options to the user and one of thems calls a JavaFXProgram to display something. I only want to run more code in the Java program when this JavaFX that got called actually exits, it may take 5 seconds, it may take a minute. Ideally what i would like is something like we have in Android. We call <code>startActivityForResult()</code> and then wait for the call of <code>onActivityResult()</code>. 
  How can i achieve similar behaviour in my situation?</p>
  
  <p>I have this code that i wrote to try to replicate the problem i has having. It's similar idea but somehow this calls JavaFX, goes to start of loop and retrieves input from the user without a problem. In my other program i always get <code>Exception in thread "main" java.util.InputMismatchException</code> when it goes back again to scan for input. But as i said, ideally, i would like to only run more code after JavaFX Application closes. </p>
  
  <pre><code>package JavaCallsJavaFXandWaits;
  
  import java.util.Scanner;
  import javafx.application.Application;
  
  public class MyJavaProgram {
      public static void main(String[] args) {
          int input;
          Scanner scanner = new Scanner(System.in);
          while (true) {
              System.out.println("0 - exit");
              System.out.println("1 - display something to me");
              input = scanner.nextInt();
              switch (input) {
                  case 0:
                      break;
                  case 1:
                      Application.launch(JavaCallsJavaFXandWaits.MyJavaFXProgram.class, null);
                      // how to get notified of MyJavaFXProgram exit? I only want to run code after it exits
                      break;
  
              }
              if (input == 0) break;
          }
  
  
      }
  
  }
  
  package JavaCallsJavaFXandWaits;
  
  import javafx.application.Application;
  import javafx.scene.Scene;
  import javafx.scene.layout.StackPane;
  import javafx.scene.text.Text;
  import javafx.stage.Stage;
  public class MyJavaFXProgram extends Application {
  
      @Override
      public void start(Stage primaryStage) {
          Text oText = new Text("My JavaFXProgram");
  
          StackPane root = new StackPane();
          root.getChildren().add(oText);
  
          Scene scene = new Scene(root, 800, 600);
  
          primaryStage.setScene(scene);
          primaryStage.show();
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  
  }
  </code></pre>
  
  <p><strong>edit1:</strong></p>
  
  <p>I just noticed that if i try to display something two times (eg: choose 1, close JavaFX application, then choose 1 again) it crashes with <code>Exception in thread "main" java.lang.IllegalStateException: Application launch must not be called more than once</code>. It seems that JavaFX Application is not exiting properly in this code too. </p>