I went through almost every post here regarding the matter but most of them doesn\'t explain what to do properly. To the question:
I created a javaFX application, a
You already noticed that you cannot do the launching process again. Therefore your best option is to rewrite the application class and move the initialisation logic to a new method:
void cleanup() {
// stop animations reset model ect.
}
void startGame(Stage stage) {
// initialisation from start method goes here
btnNewGame.setOnAction(e -> {
restart(stage);
});
stage.show();
}
void restart(Stage stage) {
cleanup();
startGame(stage);
}
@Override
public void start(Stage primaryStage) {
startGame(primaryStage);
}
launch()
is a static
method and you should not create a instance of your application class yourself for that reason. Use Application.launch(GameUI.class, args);
instead and let the method handle the creation of the GameUI
instance.Application
.