DirectoryChooser class is a part of JavaFX. DirectoryChooser class shows a directory chooser dialog which allows the user to select a particular directory. Opening a directory dialog may always result in a no-op i.e. the null file being returned. DirectoryChooser class inherits Object class.
Constructor of the class:
- DirectoryChooser() : Creates a new object of directory chooser.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| getInitialDirectory() | Returns the initialDirectory of the directory chooser. |
| getTitle() | Returns the title of directory chooser. |
| setInitialDirectory(File val) | Sets the value of the property initialDirectory |
| setTitle(String t) | Sets the title of the directory chooser. |
| showDialog(Window w) | Shows a new directory selection dialog. |
Below programs illustrate the use of the DirectoryChooser Class:
- Java Program to create DirectoryChooser and add it to the stage: In this program we will create a directory chooser named dir_chooser. Create a Label named label and a Button named button. Create an EventHandler to handle the events when the button pressed. When the button pressed, a directory chooser dialog appears and the selected directory is shown as text in the label. Add the label and the button to Vbox and add the VBox to the Scene and add the scene to the stage, and call the show() function to display the final results.
// Java Program to create DirectoryChooser// and add it to the stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.geometry.*;importjavafx.scene.paint.*;importjavafx.scene.canvas.*;importjavafx.scene.text.*;importjavafx.scene.Group;importjavafx.scene.shape.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.collections.*;importjava.io.*;importjavafx.stage.DirectoryChooser;ÂÂpublicclassDirectoryChooser_1extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Âtry{           Â// set title for the stage           Âstage.setTitle("DirectoryChooser");           Â// create a Directory chooser           ÂDirectoryChooser dir_chooser =newDirectoryChooser();           Â// create a Label           ÂLabel label =newLabel("no files selected");           Â// create a Button           ÂButton button =newButton("Show");           Â// create an Event Handler           ÂEventHandler<ActionEvent> event =           ÂnewEventHandler<ActionEvent>() {               Âpublicvoidhandle(ActionEvent e)               Â{                   Â// get the file selected                   ÂFile file = dir_chooser.showDialog(stage);                   Âif(file !=null) {                       Âlabel.setText(file.getAbsolutePath() +" selected");                   Â}               Â}           Â};           Âbutton.setOnAction(event);           Â// create a VBox           ÂVBox vbox =newVBox(30, label, button);           Â// set Alignment           Âvbox.setAlignment(Pos.CENTER);           Â// create a scene           ÂScene scene =newScene(vbox,800,500);           Â// set the scene           Âstage.setScene(scene);           Âstage.show();       Â}       Âcatch(Exception e) {           ÂSystem.out.println(e.getMessage());       Â}   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}Output:
- Java Program to create DirectoryChooser, set title, initial directory and add it to the stage: In this program we will create a directory chooser named dir_chooser. Create a Label named label and a Button named button. Set the title and initial directory of directory chooser using the setTitle() and setInitialDirectory() function. We will create a EventHandler to handle the events when the button is pressed. When the button is pressed a directory chooser dialog appears and the selected directory is shown as text in the label. Add the label and the button to Vbox and add the VBox to the Scene and add the scene to the stage, and call the show() function to display the final results.
// Java Program to create DirectoryChooser,// set title, initial directory// and add it to the stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.geometry.*;importjavafx.scene.paint.*;importjavafx.scene.canvas.*;importjavafx.scene.text.*;importjavafx.scene.Group;importjavafx.scene.shape.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.collections.*;importjava.io.*;importjavafx.stage.DirectoryChooser;ÂÂpublicclassDirectoryChooser_2extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Âtry{           Â// set title for the stage           Âstage.setTitle("DirectoryChooser");           Â// create a Directory chooser           ÂDirectoryChooser dir_chooser =                  ÂnewDirectoryChooser();           Â// set title           Âdir_chooser.setTitle("Select directory");           Â// set initial directory           Âdir_chooser.setInitialDirectory(newFile("e:\\"));           Â// create a Label           ÂLabel label =newLabel("no files selected");           Â// create a Button           ÂButton button =newButton("Show");           Â// create an Event Handler           ÂEventHandler<ActionEvent> event =           ÂnewEventHandler<ActionEvent>() {               Âpublicvoidhandle(ActionEvent e)               Â{                   Â// get the file selected                   ÂFile file = dir_chooser.showDialog(stage);                   Âif(file !=null) {                       Âlabel.setText(file.getAbsolutePath() +" selected");                   Â}               Â}           Â};           Âbutton.setOnAction(event);           Â// create a VBox           ÂVBox vbox =newVBox(30, label, button);           Â// set Alignment           Âvbox.setAlignment(Pos.CENTER);           Â// create a scene           ÂScene scene =newScene(vbox,800,500);           Â// set the scene           Âstage.setScene(scene);           Âstage.show();       Â}       Âcatch(Exception e) {           ÂSystem.out.println(e.getMessage());       Â}   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}Output:
Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/stage/DirectoryChooser.html

