Friday, November 1, 2024
Google search engine
HomeLanguagesJavaJavaFX | DirectoryChooser Class

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:

  1. 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:

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

RELATED ARTICLES

Most Popular

Recent Comments