Created
March 8, 2016 09:01
-
-
Save bugabinga/ce9e0ae2328ba34133bd to your computer and use it in GitHub Desktop.
Restarting a JavaFx App (probably unsafe)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.isp.stackoverflow; | |
import javafx.application.Application; | |
import javafx.application.Platform; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.layout.StackPane; | |
import javafx.stage.Stage; | |
/** | |
* https://stackoverflow.com/questions/35858903/how-to-reload-an-application-in-javafx | |
* | |
* @author okr | |
* @date 08.03.2016 | |
* | |
*/ | |
public class ReloadApp extends Application | |
{ | |
boolean state = true; | |
@Override | |
public void start( final Stage primaryStage ) | |
{ | |
System.out.println( "state is " + state ); | |
playGame(); | |
System.out.println( "state is " + state ); | |
final Button restartButton = new Button( "Restart" ); | |
restartButton.setOnAction( __ -> | |
{ | |
System.out.println( "Restarting app!" ); | |
primaryStage.close(); | |
Platform.runLater( () -> new ReloadApp().start( new Stage() ) ); | |
} ); | |
primaryStage.setScene( new Scene( new StackPane( restartButton ) ) ); | |
primaryStage.show(); | |
} | |
/** | |
* Simulate a game play by changing the global state. | |
*/ | |
private void playGame() | |
{ | |
state = false; | |
} | |
/** | |
* @param args ignored. | |
*/ | |
public static void main( final String[] args ) | |
{ | |
launch( args ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you!