How to get parent Window in FXML Controller?

旧城冷巷雨未停 提交于 2019-12-04 09:13:53

问题


For example, I want open a DirectoryChooser when clicking on the button:

<VBox fx:controller="com.foo.MyController"
    xmlns:fx="http://javafx.com/fxml">
    <children>
        <Button text="Click Me!" onAction="#handleButtonAction"/>
    </children>
</VBox>

And the Controller class:

package com.foo;

public class MyController {
    public void handleButtonAction(ActionEvent event) {
        DirectoryChooser dc = new DirectoryChooser();
        File folder = dc.showDialog(null);//I want to put the WIndows here.
    }
}

I want to put the main Window to the ShowDialog so that it will be blocked but how can I access it?


回答1:


you can ask any node for the Scene and then call Scene#getWindow().

E.g. ((Node)event.getTarget()).getScene().getWindow()



来源:https://stackoverflow.com/questions/13585590/how-to-get-parent-window-in-fxml-controller

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