How to get parent Window in FXML Controller?

2019-03-14 16:44发布

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?

标签: javafx-2 fxml
1条回答
闹够了就滚
2楼-- · 2019-03-14 17:23

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

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

查看更多
登录 后发表回答