DOC-03-28 文件选择框(File Chooser)
本章解释了如何使用FileChooser类来让用户浏览文件系统。在本章中提供的样例程序解释了如何打开一个或多个文件,配置一个文件选择对话框并且保存应用程序内容。
与其它UI控件类不同,FileChooser类并不属于javafx.scene.controls包。然而这个类值得在JavaFX UI控件教程中被提到,因为它支持一个经典的GUI应用程序功能:文件系统浏览。
FileChooser类在javafx.stage包中,它与其它基本根图形元素在一起,例如Stage、Windows和Popup。在图28-1中的“View Pictures”窗体是一个在Windows操作系统中的文件选择对话框样例。
图28–1文件选择框样例
打开文件
一个文件选择器可以用于调用一个“打开”对话框窗体来选择一个或多个文件,并且可以启用文件保存对话框窗体。为了展示一个文件选择框,一般可以使用FileChooser类。例28-1展示了在应用程序中启用文件选择框的最简单方式。
例28-1展现一个File Chooser
Java
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.showOpenDialog(stage);
1
2
3
FileChooserfileChooser=newFileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.showOpenDialog(stage);
在例28-1的代码添加到JavaFX应用程序之中后,当应用程序启动时将会立即显示对应的文件选择对话框窗体,如图28-2.
图28-2简单的File Chooser
注意:图28-2展示了Windows操作系统中的文件选择器。当你在其它支持这个功能的操作系统中打开文件选择器时,你将会看到不同的窗体。图28-3和图28-4展示了在Linux和Mac OS中的文件选择框样例。
图28–3 Linux操作系统中的File Chooser窗体
图28-4 Mac OS中的File Chooser
尽管在前面的样例中文件选择器都会在应用程序启动时自动出现,但是一个更为典型的应用方式是在选择了对应的菜单项或点击了特定的按钮时调用文件选择框。在本教程中,你会创建一个应用程序,它会允许用户点击一个按钮并且打开文件系统中的一张或多张图片。例28-2展示了FileChooserSample应用程序的代码,它实现了这个功能。
例28–2打开FileChooser来选择一个或多个文件
Java
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public final class FileChooserSample extends Application {
private final Desktop desktop = Desktop.getDesktop();
@Override
public void start(final Stage stage) {
stage.setTitle("File Chooser Sample");
final FileChooser fileChooser = new FileChooser();
final Button openButton = new Button("Open a Picture...");
final Button openMultipleButton = new Button("Open Pictures...");
openButton.setOnAction(
(final ActionEvent e) -> {
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
openFile(file);
}
});
openMultipleButton.setOnAction(
(final ActionEvent e) -> {
List list =
fileChooser.showOpenMultipleDialog(stage);
if (list != null) {
list.stream().forEach((file) -> {
openFile(file);
});
}
});
final GridPane inputGridPane = new GridPane();
GridPane.setConstraints(openButton, 0, 0);
GridPane.setConstraints(openMultipleButton, 1, 0);
inputGridPane.setHgap(6);
inputGridPane.setVgap(6);
inputGridPane.getChildren().addAll(openButton, openMultipleButton);
final Pane rootGroup = new VBox(12);
rootGroup.getChildren().addAll(inputGridPane);
rootGroup.setPadding(new Insets(12, 12, 12, 12));
stage.setScene(new Scene(rootGroup));
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
private void openFile(File file) {
EventQueue.invokeLater(() -> {
try {
desktop.open(file);
} catch (IOException ex) {
Logger.getLogger(FileChooserSample.
class.getName()).
log(Level.SEVERE, null, ex);
}
});
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
importjava.awt.Desktop;
impor