在主窗口中打开文件选择器 - Javafx [英] Opening a file chooser in the primary window - Javafx

查看:288
本文介绍了在主窗口中打开文件选择器 - Javafx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下代码打开文件选择器

I am able to open the file chooser with the following code

    @FXML
    private TextField myText;

    @FXML
    private Button browse;
    private Window primaryStage;


    @FXML
    private void initialize(){

        browse.setOnAction((event) -> {
            FileChooser fileChooser = new FileChooser();
            File file = fileChooser.showOpenDialog(primaryStage);
            String fileName = String.valueOf(file);
            myText.setText(fileName);
        });

    }

但我试图打开这样的文件选择器(Microsoft更新程序的屏幕截图)

But I am trying to open the file chooser something like this (screenshot of Microsoft updater)

任何人都可以告诉我如何打开文件选择器,如图所示在屏幕截图中(如顶层)?

Can anyone tell me how should I open the file chooser as shown in the screen shot (like a top layer)?

谢谢。

推荐答案

您的文件选择器没有附加到 primaryStage 的原因>是因为 primaryStage null 。你永远不会设定它的价值。当这样的参数是 null 时,文件选择器将在没有父项的情况下浮动。

The reason your file chooser is not being attached to your primaryStage is because primaryStage is null. You never set its value. When such parameter is null, the file chooser will float around without a parent.

所以你的解决方案是实际设置 primaryStage 。但是如果你想要一个快速的解决方案,用这个替换文件file = fileChooser.showOpenDialog(primaryStage);

So your solution would be to actually set primaryStage. But if you want a quick solution, replace File file = fileChooser.showOpenDialog(primaryStage); with this:

File file = fileChooser.showOpenDialog(((Node) event.getTarget()).getScene().getWindow());

这篇关于在主窗口中打开文件选择器 - Javafx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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