如何在FXML控制器中获取父窗口?

时间:2012-11-27 13:44:28

标签: javafx-2 fxml

例如,我想在点击按钮时打开DirectoryChooser:

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

Controller类:

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.
    }
}

我想将主窗口放到 ShowDialog 中,以便它被阻止但我该如何访问它?

1 个答案:

答案 0 :(得分:40)

您可以向任何节点询问Scene,然后致电Scene#getWindow()

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