和其他的接口组件不同,FileChooser 不属于javafx.scene.controls包,FileChooser 类在javafx.stage 包下,和其他主要的图形元素在一起,比如:Stage,Window,以及Popup。
FileChooser提供了一个典型的界面功能:文件系统导航。
文件选择器经常用来打开一个窗口来选择文件或者保存文件,最简单是文件选择器的使用如下面代码所示:
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.showOpenDialog(stage);
FileChooser最常用的还是让我们点击一个按钮后弹出文件选择器,看下面这个例子:
import java.awt.Desktop;
import java.awt.EventQueue;
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);
}
});
}
}
在该例子中,Open a Picture按钮用来打开一个可以选择一个图片的文件选择器,Open Pictures 按钮可以打开一个可以选择多个图片的文件选择器,setOnAction 方法定义了它们打开按钮的方式。
在下面的例子调用了initialDirectory 方法来初始化路径,以及设置title属性。
import java.awt.Desktop;
import java.awt.EventQueue;
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) -> {
configureFileChooser(fileChooser);
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
openFile(file);
}
}); openMultipleButton.setOnAction(
(final ActionEvent e) -> {
configureFileChooser(fileChooser);
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 static void configureFileChooser(final FileChooser fileChooser){
fileChooser.setTitle("View Pictures");
fileChooser.setInitialDirectory(
new File(System.getProperty("user.home"))
);
}
private void openFile(File file) {
EventQueue.invokeLater(() -> {
try {
desktop.open(file);
} catch (IOException ex) {
Logger.getLogger(FileChooserSample.
class.getName()).
log(Level.SEVERE, null, ex);
}
});
}
}
import java.awt.Desktop;
import java.awt.EventQueue;
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) -> {
configureFileChooser(fileChooser);
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
openFile(file);
}
});
openMultipleButton.setOnAction(
(final ActionEvent e) -> {
configureFileChooser(fileChooser);
List list =
fileChooser.showOpenMultipleDialog(stage);
if (list != null) {
list.stream().forEach((file) -> {
openFile(file);
});
}
});
final GridPane inputGridPane = new GridPane();
GridPane.setConstraints(openButton, 0, 1);
GridPane.setConstraints(openMultipleButton, 1, 1);
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 static void configureFileChooser(
final FileChooser fileChooser) {
fileChooser.setTitle("View Pictures");
fileChooser.setInitialDirectory(
new File(System.getProperty("user.home"))
);
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("All Images", "*.*"),
new FileChooser.ExtensionFilter("JPG", "*.jpg"),
new FileChooser.ExtensionFilter("PNG", "*.png")
);
}
private void openFile(File file) {
EventQueue.invokeLater(() -> {
try {
desktop.open(file);
} catch (IOException ex) {
Logger.getLogger(FileChooserSample.
class.getName()).
log(Level.SEVERE, null, ex);
}
});
}
}
上面的代码使用FileChooser.ExtensionFilter来设置了一个extension filter,它定义了这些文件类型是可以选择的:所有的图片文件、JPG和PNG。
当你编译、运行上面的代码时,点击其中的一个按钮,对应的扩展名过滤器将会出现在文件选择框窗体中。如果一个用户选择了JPG,那么在文件选择框中将会仅显示JPG类型的图片。
除了打开和过滤文件之外,FileChooser 还提供了保存文件的能力。
FileChooser fileChooser = new FileChooser();
File file = fileChooser.showSaveDialog(stage);
if(file != null){
//将要写入的内容写到file文件中
}
FileChooser类的showSaveDialog方法会打开一个保存对话框窗体。与其他展示对话框的方法一样,showSaveDialog方法返回了被用户选择的文件或者当没有作出选择时返回null。