JavaFX FileChooser initial directory

青春壹個敷衍的年華 提交于 2019-12-12 03:17:46

问题


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?


回答1:


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

Then you can just do

FileChooser chooser = MyUtilities.createFileChooser();

whenever you need one.

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.



来源:https://stackoverflow.com/questions/32318974/javafx-filechooser-initial-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!