JavaFX FileChooser select files and/or directories?


Question

JavaFX allows selecting a file via FileChooser and selecting a directory via DirectoryChooser, but how do I allow it to select both at once?

Something like Swing's JFileChooser.FILES_AND_DIRECTORIES option.

Currently I'm just using JFileChooser directly, but it's not exactly a good visual match:

JFileChooser chooser = new JFileChooser(".");
chooser.setMultiSelectionEnabled(true);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int ret = chooser.showOpenDialog(null);
if(ret == JFileChooser.APPROVE_OPTION) {
    File[] files = chooser.getSelectedFiles();
    ...
}
1
14
9/28/2012 5:49:21 PM

Accepted Answer

JavaFX allows selecting a file via FileChooser and selecting a directory via DirectoryChooser, but how do I allow it to select both at once?

There is no such functionality in JavaFX 2.2 or current JavaFX 8 builds.


A FILES_AND_DIRECTORIES chooser for JavaFX was requested in RT-22621.

The request was declined as Won't Fix for the following reason:

Lubomir Nerad added a comment - Jun, 19 2012 06:59 AM JavaFX uses the corresponding native dialogs for its file and directory chooser. Unless the native file dialogs on all supported platforms can provide this feature, we won't be able to add it to the API.

Lubomir Nerad added a comment - Jul, 24 2012 06:54 AM The requested functionality is not provided by the platform dialogs on Window XP and Linux/GTK.

For Java 8, Windows XP is no longer supported and perhaps Linux will be enhanced to natively provide such functionality so that JavaFX could use it and such a feature could be provided in JavaFX in the future. You may comment on the linked Jira case to register your interest.

16
7/16/2015 6:24:12 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon