如何在FXML Controller中获取父窗口? [英] How to get parent Window in FXML Controller?

查看:440
本文介绍了如何在FXML Controller中获取父窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

和Controller类:

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

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

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

推荐答案

您可以向任何节点请求Scene,然后调用Scene#getWindow().

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

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

这篇关于如何在FXML Controller中获取父窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆