JavaFX FileChooser

newhouse picture newhouse · Feb 21, 2012 · Viewed 51.3k times · Source

I came across a little problem now with JavaFX. I tried to make a filechoosing in my code where I can point on a FOLDER instead of a file. Currently I don't have a solution for it. Do you know a workaround (except using JFileChooser from swing)?

Many thanks for the answers in advance

edit: I already got now an answer, trying to test it, but I forgot to mention the version of JavaFX. It is the latest 2.0.3 stable here, released a few days ago (but the initial non-beta 2.0 and 2.0.1 had this issue also)

Answer

jewelsea picture jewelsea · Feb 22, 2012

A DirectoryChooser was added to JavaFX as part of the 2.1 release. Usage is:

DirectoryChooser chooser = new DirectoryChooser();
chooser.setTitle("JavaFX Projects");
File defaultDirectory = new File("c:/dev/javafx");
chooser.setInitialDirectory(defaultDirectory);
File selectedDirectory = chooser.showDialog(primaryStage);

The issue tracker mentions a work-around for the 2.0GA release: "accessing the private Oracle API Glass method CommonDialogs.showFolderChooser".