我想做一些事情,在控制器的initialize()方法完成之后,但在场景显示之前。在场景显示之前是否会调用任何方法?我想在该方法中添加一些代码。
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("sample.fxml"));
AnchorPane pane = loader.load();
Scene gameScene = new Scene(pane);
//I load a secne above,the I will get the controller,set some properties,then,use the properties to read a file before the secene show.
GameUIController controller = loader.getController();
controller.setGameFileLoacation("game1.txt");//I set a property here.I want to use it to read game file,and load game,set some necessary UI.
primaryStage.setScene(gameScene);//this tow statement will show the scene.
primaryStage.show();
我无法将代码放入initialize()方法中,因为它会在加载fxml文件时调用(当我尚未获取控制器时)。那么,我该怎么办?
非常感谢 !
我找到的一种解决方案
primaryStage.addEventHandler(WindowEvent.WINDOW_SHOWING, new EventHandler<WindowEvent>()
{
@Override
public void handle(WindowEvent window)
{
//Your code
}
});
该事件发生在窗口显示之前。doc link
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)