JavaFX DirectoryChooser

107 篇文章 6 订阅
本文介绍如何在JavaFX中创建DirectoryChooser,设置初始目录并显示对话框,以便用户选择本地文件夹。
摘要由CSDN通过智能技术生成

A JavaFX DirectoryChooser is a dialog that enables the user to select a directory via a file explorer from the user's local computer. The JavaFX DirectoryChooser is implemented in the class javafx.stage.DirectoryChooser. In this JavaFX DirectoryChooser tutorial I will show you how to use the DirectoryChooser dialog.

Here is an example screenshot of how a JavaFX DirectoryChooser looks:

Creating a DirectoryChooser

In order to use the DirectoryChooser you must first create a DirectoryChooser instance. Here is an example of creating a JavaFX DirectoryChooser:

DirectoryChooser directoryChooser = new DirectoryChooser();

Showing the DirectoryChooser Dialog

In order to make the DirectoryChooser visible you must call its showDialog() method. Here is an example of showing a JavaFX DirectoryChooser:

File selectedDirectory = directoryChooser.showDialog(primaryStage);

The File returned by the showDialog() method represents the directory the user selected in the DirectoryChooser.

The stage parameter is the JavaFX Stage that should "own" the DirectoryChooser dialog. By "owning" is meant what Stage from which the DirectoryChooser dialog is shown. This will typically be the Stage in which the button sits that initiates the showing of the DirectoryChooser.

Showing a DirectoryChooser is typically done as a result of a click on a button or menu item. Here is a full JavaFX example that shows a button that opens a DirectoryChooser when it is clicked:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

import java.io.File;

public class DirectoryChooserExample extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX App");

        DirectoryChooser directoryChooser = new DirectoryChooser();
        directoryChooser.setInitialDirectory(new File("src"));

        Button button = new Button("Select Directory");
        button.setOnAction(e -> {
            File selectedDirectory = directoryChooser.showDialog(primaryStage);

            System.out.println(selectedDirectory.getAbsolutePath());
        });


        VBox vBox = new VBox(button);
        //HBox hBox = new HBox(button1, button2);
        Scene scene = new Scene(vBox, 960, 600);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

Setting Initial Directory

You can set the initial directory of the JavaFX DirectoryChooser, meaning the root directory the DirectoryChooser will be located at when opened. This is also shown in the example above. You set the initial directory via the method setInitialDirectory(). Here is an example of setting the initial directory of a JavaFX DirectoryChooser:

directoryChooser.setInitialDirectory(new File("data/json/invoices"));

This example will set the initial directory of the given DirectoryChooser to data/json/invoices .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值