JavaFX FileChooser的初始目录 [英] JavaFX FileChooser initial directory

查看:1335
本文介绍了JavaFX FileChooser的初始目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swing中,JFileChooser指向用户的默认目录,该目录通常是Windows中的My Documents文件夹。默认情况下,JavaFX FileChooser没有相同的行为。有一个 setInitialDirectory 方法应该没问题,但是我们打开FileChoosers的应用程序中有很多地方。不幸的是,FileChooser类是final,所以我不能简单地扩展类,只需调用 setInitialDirectory 一次。除了浏览整个应用程序并添加 setInitialDirectory 调用之外,还有什么我可以做的吗?

In Swing, the JFileChooser pointed to the user's default directory which is typically the "My Documents" folder in Windows. The JavaFX FileChooser does not have the same behavior by default. There is a setInitialDirectory method which should be fine, however there are a number of places in the application that we open FileChoosers. Unfortunately the FileChooser class is final, so I cannot simply extend the class and just call the setInitialDirectory once. Is there anything else I could do besides going through the entire application and adding the setInitialDirectory calls?

推荐答案

有一个明显的解决方案,只需在某处创建静态实用程序方法:

There's the obvious solution, to just create a static utility method somewhere:

public class MyUtilities {

    public static FileChooser createFileChooser() {
        FileChooser chooser = new FileChooser();
        chooser.setInitialDirectory(new File(System.getProperty("user.home"));
        return chooser ;
    }
}

然后你可以做到

FileChooser chooser = MyUtilities.createFileChooser();

无论何时需要。

从用户体验的角度来看,我实际上更喜欢为整个应用程序使用单个 FileChooser 实例(或至少对于大型的每个功能部分)这样它就维护了用户访问的最后一个目录,这对我来说更方便。

I actually prefer, from a user experience perspective, to use a single FileChooser instance for the whole application (or at least for each functional portion of a large application). That way it maintains the last directory the user visited, which is more convenient imho.

这篇关于JavaFX FileChooser的初始目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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