Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX FileChooser

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)

like image 977
newhouse Avatar asked Feb 21 '12 10:02

newhouse


People also ask

What is FileChooser in JavaFX?

FileChooser class is a part of JavaFX. It is used to invoke file open dialogs for selecting a single file (showOpenDialog), file open dialogs for selecting multiple files (showOpenMultipleDialog) and file save dialogs (showSaveDialog). FileChooser class inherits Object class.

How to add file chooser in JavaFX?

FileChooser fileChooser = new FileChooser(); fileChooser. setTitle("Open Resource File"); fileChooser. showOpenDialog(stage); After the code from Example 26-1 is added to a JavaFX application, the file chooser dialog window appears immediately when the application starts, as shown in Figure 26-2.

What is a FileChooser?

FileChooser ) is a dialog that enables the user to select one or more files via a file explorer from the user's local computer. The JavaFX FileChooser is implemented in the class javafx. stage. FileChooser .


2 Answers

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

like image 72
jewelsea Avatar answered Sep 20 '22 17:09

jewelsea


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

like image 43
Emmanuel Ogoma Avatar answered Sep 19 '22 17:09

Emmanuel Ogoma