67

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)

jewelsea
  • 150,031
  • 14
  • 366
  • 406
newhouse
  • 1,152
  • 1
  • 10
  • 27

2 Answers2

125

A FileChooser is available as part of the JavaFX API.

Example usage from javadoc:

FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");

fileChooser.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 = fileChooser.showOpenDialog(mainStage);

if (selectedFile != null) {
    mainStage.display(selectedFile);
}

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".


Both the DirectoryChooser and FileChooser will internally be implemented using the native file and directory choosing user interface dialogs provided by the Operating System (they are not actually implemented as JavaFX stages with a SceneGraphs).

This means that the look and feel of these elements will differ depending on the operating system platform and native window management toolkit that JavaFX is running on top of.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • 2
    "You cannot view this URL as a guest. You must log in or sign up for an account." – XXL Apr 21 '12 at 13:38
  • 2
    Click the sign up link in the [JavaFX Jira tracker](http://javafx-jira.kenai.com/) to sign up for an account - it's an open system, anybody can sign up for an account. – jewelsea Apr 23 '12 at 21:30
  • 2
    Policy changed a couple of months back and the JavaFX Jira tracker is no longer an open system. Future bugs reports may be filed to: http://bugreport.java.com and some version of them might eventually show up for viewing in https://bugs.openjdk.java.net – jewelsea May 28 '15 at 21:47
7

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

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
Emmanuel Ogoma
  • 584
  • 5
  • 12
  • Something funny here: maybe you could improve your answer (otherwise you will be confusing people). In your example supposedly of a `DirectoryChooser` you are not using a `javafx.stage.DirectoryChooser`. Furthermore it does not have a method `showOpenDialog`. – mike rodent Feb 15 '18 at 19:28
  • Actually I hadn't spotted DirectoryChooser... so thanks! – mike rodent Feb 16 '18 at 20:50