我想从我的控制器类处理阶段事件(即隐藏).所以我要做的就是通过添加一个监听器
((Stage)myPane.getScene().getWindow()).setOn*whatIwant*(...);
Run Code Online (Sandbox Code Playgroud)
但问题是初始化刚刚开始
Parent root = FXMLLoader.load(getClass().getResource("MyGui.fxml"));
Run Code Online (Sandbox Code Playgroud)
之前
Scene scene = new Scene(root);
stage.setScene(scene);
Run Code Online (Sandbox Code Playgroud)
因此.getScene()返回null.
我自己找到的唯一解决方法是向myPane.sceneProperty()添加一个监听器,当它变为非null时,我得到场景,添加到它的.windowProperty()我的!该死!我最终检索阶段的监听器处理.这一切都以设置所需的听众来举办舞台活动而告终.我认为听众太多了.这是解决我问题的唯一方法吗?
我的问题是所有使用的例子都FileChooser要求你传递一个阶段.唯一的问题是我的UI是在一个fxml文件中定义的,该文件使用与主阶段分开的控制器类.
@FXML protected void locateFile(ActionEvent event) {
FileChooser chooser = new FileChooser();
chooser.setTitle("Open File");
chooser.showOpenDialog(???);
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做???才能让它发挥作用?就像我说的那样,我对控制器类中的任何阶段都没有任何引用,所以我该怎么办?