27

例如,我想在单击按钮时打开一个 DirectoryChooser:

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

和控制器类:

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中,这样它就会被阻止,但我怎样才能访问它呢?

4

1 回答 1

54

您可以向任何节点询问Scene然后调用Scene#getWindow().

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

如果这是一个处理程序,则来自@osveinMenuItem ,它应该是:

((MenuItem) event.getTarget()).getParentPopup().getOwnerWindow()
于 2012-11-27T18:07:02.203 回答