Javafx 简明教程
JavaFX - FileChooser
一个 file chooser 是一个图形用户界面元素,它允许用户浏览文件系统。通常,它被用来打开或保存单个或多个文件。在下面的图中,我们可以在谷歌云端硬盘应用程序中看到一个文件选择器 -
A file chooser is a graphical user interface elements that allow users to browse through file system. Generally, it is used to open and save either single or multiple files. In the below figure, we can see a file chooser in google drive application −

FileChooser in JavaFX
在 JavaFX 中,文件选择器由一个名为 FileChooser 的类表示,这个类属于一个名为 javafx.scene.control 的包。我们可以通过实例化这个类在我们的 JavaFX 应用程序中创建一个文件选择器组件。这个类仅有一个构造函数,即它的默认构造函数。但是,它提供了多个属性,它们列在下面:
In JavaFX, the file chooser is represented by a class named FileChooser which belongs to a package named javafx.scene.control. We can create a file chooser component within our JavaFX application by instantiating this class. This class has only one constructor, i.e. its default constructor. However, it provides multiple properties, which are listed below −
-
initialDirectory − This property specifies the initial directory of the file chooser. You can set value to it using the setInitialDirectory() method.
-
selectedExtensionFilter − This property specifies the extension filter displayed in the dialog. You can set value to it using the setSelectedExtensionFilter() method.
-
Title − The property specifies the title of the dialog. can set value to it using the * setTitle()* method.
How to create a FileChooser in JavaFX?
按照下面给出的步骤在 JavaFX 中创建一个文件选择器。
Follow the steps given below to create a file chooser in JavaFX.
Step 1: Create a node to associate the FileChooser
一个文件选择器必须与另外一个节点相关联,比如菜单或按钮,这样当点击该节点时,一个对话框窗口将会打开以供选择文件。为此目的,我们使用了一个菜单,就像下面的代码中所示:
A file chooser must be associated with another node, like menu or button, so that when the node is clicked, a dialog window opens for file selection. For this purpose, we have used a Menu as shown in the below code −
//Creating a menu
Menu fileMenu = new Menu("File");
//Creating menu Items
MenuItem item = new MenuItem("Open Image");
Step 2: Instantiate the FileChooser class
要创建一个文件选择器,实例化 FileChooser 类。然后,借助 getExtensionFilters() 方法设置所需的文件扩展名,就像在下面的代码块中所示:
To create a file chooser, instantiate the FileChooser class. Then, set the desired file extension with the help of getExtensionFilters() method as shown in the following code block −
//Creating a File chooser
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Image");
fileChooser.getExtensionFilters().addAll(new ExtensionFilter("All Files", "*.*"));
Step 3: Add action handler to the Menu
将 menu 设置为 action 很重要,因为它将触发打开文件选择器对话框。
It is important to set an action to the menu as it will trigger the opening of the file chooser dialog.
//Adding action on the menu item
item.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
//Opening a dialog box
fileChooser.showOpenDialog(stage);
}});
Step 4: Launch the Application
一旦创建文件选择器并设置好其属性,创建一个 MenuBar 。接下来,将 Menubar 对象传递给 Group 类构造函数。接着,设置 Scene 和 Stage 。最后,使用 launch() 方法启动该应用程序。
Once the file chooser is created and its properties are set, create a MenuBar. Next, pass the Menubar object to the constructor of Group class. Then, set the Scene and Stage. Lastly, launch the application with the help of the launch() method.
Example
以下是使用 JavaFX 创建一个 FileChooser 的程序。将此代码保存在名为 JavafxFilechooser.java 的文件中。
Following is the program that will create a FileChooser using JavaFX. Save this code in a file with the name JavafxFilechooser.java.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.FileChooser.ExtensionFilter;
public class JavafxFilechooser extends Application {
public void start(Stage stage) {
//Creating a menu
Menu fileMenu = new Menu("File");
//Creating menu Items
MenuItem item = new MenuItem("Open Image");
fileMenu.getItems().addAll(item);
//Creating a File chooser
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Image");
fileChooser.getExtensionFilters().addAll(new ExtensionFilter("All Files", "*.*"));
//Adding action on the menu item
item.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
//Opening a dialog box
fileChooser.showOpenDialog(stage);
}});
//Creating a menu bar and adding menu to it.
MenuBar menuBar = new MenuBar(fileMenu);
menuBar.setTranslateX(3);
menuBar.setTranslateY(3);
//Setting the stage
Group root = new Group(menuBar);
Scene scene = new Scene(root, 400, 300, Color.BEIGE);
stage.setTitle("File Chooser Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
使用以下命令从命令提示符处编译并执行已保存的 Java 文件。
Compile and execute the saved Java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxFilechooser.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxFilechooser
在执行时,以上程序会显示一个按钮。当我们点击该按钮时,它将显示一个弹出窗口,允许用户选择一个文件。
On executing, the above program displays a Button. When we click that button, it will show a pop-up window allowing the user to choose a file.
