JavaFX FileChooser
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
".
Unlike in swing where JFileChooser was being used to select folders and individual files, in javafx,there is FileChooser class which is used for choosing individual files and DirectoryChooser class for selecting directory
DirectoryChooser directoryChooser = new DirectoryChooser();
directoryChooser.setTitle("Open Resource File");
directoryChooser.getExtensionFilters().addAll(
new ExtensionFilter("Text Files", "*.txt"),
new ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"),
new ExtensionFilter("Audio Files", "*.wav", "*.mp3", "*.aac"),
new ExtensionFilter("All Files", "*.*"));
File selectedFile = directoryChooser.showDialog(mainStage);
if (selectedFile != null) {
mainStage.display(selectedFile);
}
is an example of a Directory chooser.
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Folder");
fileChooser.showDialog(stage);
is an example of file chooser