JavaFX | DirectoryChooser Class
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 applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("DirectoryChooser");// create a Directory chooserDirectoryChooser dir_chooser =newDirectoryChooser();// create a LabelLabel label =newLabel("no files selected");// create a ButtonButton button =newButton("Show");// create an Event HandlerEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// get the file selectedFile file = dir_chooser.showDialog(stage);if(file !=null) {label.setText(file.getAbsolutePath() +" selected");}}};button.setOnAction(event);// create a VBoxVBox vbox =newVBox(30, label, button);// set Alignmentvbox.setAlignment(Pos.CENTER);// create a sceneScene scene =newScene(vbox,800,500);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:


- 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 applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("DirectoryChooser");// create a Directory chooserDirectoryChooser dir_chooser =newDirectoryChooser();// set titledir_chooser.setTitle("Select directory");// set initial directorydir_chooser.setInitialDirectory(newFile("e:\\"));// create a LabelLabel label =newLabel("no files selected");// create a ButtonButton button =newButton("Show");// create an Event HandlerEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// get the file selectedFile file = dir_chooser.showDialog(stage);if(file !=null) {label.setText(file.getAbsolutePath() +" selected");}}};button.setOnAction(event);// create a VBoxVBox vbox =newVBox(30, label, button);// set Alignmentvbox.setAlignment(Pos.CENTER);// create a sceneScene scene =newScene(vbox,800,500);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:


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
Recommended Posts:
- JavaFX | Pos Class
- JavaFX | Tab Class
- JavaFX | InnerShadow Class
- JavaFX | ColorInput Class
- JavaFX | HTMLEditor Class
- JavaFX | SepiaTone Class
- JavaFX | ColorAdjust Class
- JavaFX | Rectangle2D Class
- JavaFX | SplitPane Class
- JavaFX | TitledPane Class
- JavaFX | Duration Class
- JavaFX | ToggleButton Class
- JavaFX | Popup Class
- JavaFX | Point2D Class
- JavaFX | Dimension2D Class
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



