如何从控制器类打开JavaFX FileChooser? [英] How do I open the JavaFX FileChooser from a controller class?

查看:184
本文介绍了如何从控制器类打开JavaFX FileChooser?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是使用 FileChooser 的所有示例都要求您传入一个阶段。唯一的问题是我的UI是在 fxml 文件中定义的,该文件使用与主舞台分开的控制器类。

My problem is that all the examples of using FileChooser requires you to pass in a stage. Only problem is that my UI is defined in an fxml file, which uses a controller class separate from the main stage.

@FXML protected void locateFile(ActionEvent event) {
    FileChooser chooser = new FileChooser();
    chooser.setTitle("Open File");
    chooser.showOpenDialog(???);
}

我在 ??? 让它工作?就像我说的那样,我对控制器类中的任何阶段都没有任何引用,所以该怎么办?

What do I put at the ??? to make it work? Like I said, I don't have any references to any stages in the controller class, so what do I do?

推荐答案

对于场景中的任何节点(例如,根节点;但是使用 @FXML 注入的任何节点都可以),请执行

For any node in your scene (for example, the root node; but any node you have injected with @FXML will do), do

chooser.showOpenDialog(node.getScene().getWindow());

这篇关于如何从控制器类打开JavaFX FileChooser?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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