0

My project is finished, so I created a JAR in IntelliJ. The first stage (javaFX) is showing, but when I click on the button to create a new stage nothing happens (expect the old stage closes which is expected). Why is the runtime not creating a new stage?

view.getStartButton().setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            view.getScene().getWindow().hide();
            GameView view = new GameView();
            TetrisSpelEigenschappen model = new TetrisSpelEigenschappen();
            TetrisSpel spel = new TetrisSpel(model);
            TetrisBlokken blokmodel = new TetrisBlokken(spel,model);
            GamePresenter gamePresenter = new GamePresenter(view, model, spel, blokmodel);
            Stage gameStage = new Stage();
            Scene gameScene = new Scene(view);
            gameStage.setScene(gameScene);
            gameStage.setTitle("Game");
            gameStage.centerOnScreen();
            gamePresenter.addWindowEventHandlers();
            gameStage.showAndWait();
        }
m4t5k4
  • 55
  • 9
  • To see if any unchecked exceptions are being thrown by your code, try running the jar file from the command line (`java -jar myJarFile.jar`). – James_D Mar 16 '17 at 12:57

1 Answers1

1

By default, the application will exit when the last window is closed. Since in your code you close the existing window before opening the new one, I suspect that what is happening is that the platform is exiting in the time in between. (Note this behavior is a little fragile...)

Add the line

Platform.setImplicitExit(false);

to your Application class's init() or start(...) method.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • Doesn't seem to change anything. It still closes after pressing the button. I also tried putting the getWindow.hide() in comment. The JAR no longer closes but clicking the button has no effect, no new stage. – m4t5k4 Mar 16 '17 at 12:54
  • @m4t5k4 So you're probably getting an exception thrown between the call to `hide()` and the call to `showAndWait()`. If that's happening in the jar file, but not when you run from the file system, the most likely (but not the only possible) cause is that you're loading a resource somewhere with an invalid resource name. Run the jar file from the command line so that you can see any unhandled exceptions. – James_D Mar 16 '17 at 13:17
  • Little update: the JAR file can't find the resources because of absolute paths. – m4t5k4 Mar 16 '17 at 22:23
  • @m4t5k4 See http://stackoverflow.com/questions/34755630/javafx-location-is-not-set-error (not exactly the same but may help) – James_D Mar 16 '17 at 22:24