How to reload an Application in JavaFX?
I would certainly recommend a clean approach as discussed in the questions you linked.
A quick and dirty way however might be the following:
restartButton.setOnAction( __ ->
{
System.out.println( "Restarting app!" );
primaryStage.close();
Platform.runLater( () -> new ReloadApp().start( new Stage() ) );
} );
Close the main stage, create a new one and pass it to a new instance of your App. I cannot make any guarantees about the memory behavior of this hack. Use it carefully.
Full example: https://gist.github.com/bugabinga/ce9e0ae2328ba34133bd
Maihan Nijat
Updated on June 07, 2022Comments
-
Maihan Nijat almost 2 years
I have a small game which has New Game button. There are many variables which need reset when the New Game button is pressed. Is there any method which can easily reload the whole application or any alternative to refresh the scene, stage or variables? I just want to bring the application to it's initial stage when It's first loaded.
I went through different topics on internet and read many questions and answers here also, but I there is no easy method to implement it. Most of the answers suggest to reuse the whole code or put the whole code in class and reload it.
Questions reviewed:
-
StuPointerException almost 6 yearsThanks, 2 years later, this has really helped in implementing a
debug
toggle switch. I agree with your "use it carefully" caveat! -
Oliver Jan Krylow almost 6 yearsKool :) definitly let us now when this blows up ;P