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

查看:35
本文介绍了如何从控制器类打开 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天全站免登陆