JavaFX Window Changer using FXML

好久不见. 提交于 2019-12-02 07:39:06

For a dynamically changing stage, you can (I'm currently using this method) have an AnchorPane. Say, there is an AnchorPane on top of your root. You can change the scene using this pane. First, declare the AnchorPane in your controller :

@FXML
AnchorPane dynamicPane;

Then, you should provide a method (a setter specifically), where it would look like,

private void setDynamicPane(AnchorPane dynamicPane){
      this.dynamicPane.getChildren().clear();
      this.dynamicPane.getChildren().add(dynamicPane);
}

Then it's all done, and now you can change you scene by simply calling it in a button's ActionEvent as following,

@FXML
private void yourButtonAction(ActionEvent evt){
    setDynamicPane(FXMLLoader.load(getClass().getResources("path/to/your/file.fxml"));
}

That's all!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!