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 stage
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.geometry.*;
import
javafx.scene.paint.*;
import
javafx.scene.canvas.*;
import
javafx.scene.text.*;
import
javafx.scene.Group;
import
javafx.scene.shape.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.collections.*;
import
java.io.*;
import
javafx.stage.DirectoryChooser;
Â
Âpublic
class
DirectoryChooser_1
extends
Application {
Â
ÂÂ Â Â Â
// launch the application
   Â
public
void
start(Stage stage)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
try
{
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set title for the stage
           Â
stage.setTitle(
"DirectoryChooser"
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create a Directory chooser
           Â
DirectoryChooser dir_chooser =
new
DirectoryChooser();
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create a Label
           Â
Label label =
new
Label(
"no files selected"
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create a Button
           Â
Button button =
new
Button(
"Show"
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create an Event Handler
           Â
EventHandler<ActionEvent> event =Â
           Â
new
EventHandler<ActionEvent>() {
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
public
void
handle(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 =
new
VBox(
30
, label, button);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set Alignment
           Â
vbox.setAlignment(Pos.CENTER);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create a scene
           Â
Scene scene =
new
Scene(vbox,
800
,
500
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set the scene
           Â
stage.setScene(scene);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
stage.show();
       Â
}
Â
ÂÂ Â Â Â Â Â Â Â
catch
(Exception e) {
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
System.out.println(e.getMessage());
       Â
}
   Â
}
Â
ÂÂ Â Â Â
// Main Method
   Â
public
static
void
main(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 stage
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.geometry.*;
import
javafx.scene.paint.*;
import
javafx.scene.canvas.*;
import
javafx.scene.text.*;
import
javafx.scene.Group;
import
javafx.scene.shape.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.collections.*;
import
java.io.*;
import
javafx.stage.DirectoryChooser;
Â
Âpublic
class
DirectoryChooser_2
extends
Application {
Â
ÂÂ Â Â Â
// launch the application
   Â
public
void
start(Stage stage)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
try
{
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set title for the stage
           Â
stage.setTitle(
"DirectoryChooser"
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create a Directory chooser
           Â
DirectoryChooser dir_chooser =Â
                  Â
new
DirectoryChooser();
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set title
           Â
dir_chooser.setTitle(
"Select directory"
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set initial directory
           Â
dir_chooser.setInitialDirectory(
new
File(
"e:\\"
));
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create a Label
           Â
Label label =
new
Label(
"no files selected"
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create a Button
           Â
Button button =
new
Button(
"Show"
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create an Event Handler
           Â
EventHandler<ActionEvent> event =
           Â
new
EventHandler<ActionEvent>() {
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
public
void
handle(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 =
new
VBox(
30
, label, button);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set Alignment
           Â
vbox.setAlignment(Pos.CENTER);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create a scene
           Â
Scene scene =
new
Scene(vbox,
800
,
500
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set the scene
           Â
stage.setScene(scene);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
stage.show();
       Â
}
Â
ÂÂ Â Â Â Â Â Â Â
catch
(Exception e) {
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
System.out.println(e.getMessage());
       Â
}
   Â
}
Â
ÂÂ Â Â Â
// Main Method
   Â
public
static
void
main(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