How to reload an Application in JavaFX?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 23:58:56

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

for reload application you must create new instance of your main class and call the start method of it with stage parameter.for example

restartButton.setOnAction(e->{yourAPP app=new yourApp();
app.start(yourStage);});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!