RootLayoutController.java
@FXML
private void handleOpen(){
FileChooser fileChooser = new FileChooser();
//Set extension filter
ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Image Files", "*.png","*.jpg", "*.jpeg");
fileChooser.getExtensionFilters().add(extFilter);
// Show save file dialog
List<File> list = fileChooser.showOpenMultipleDialog(mainApp.getPrimaryStage());
if(list != null){
for(File file : list){
imageView.createImageView(file);
}
}
}
ImageViewController.java
public ImageView createImageView(File file){
imageView.getImage();
String path = file.getAbsolutePath();
try{
final Image image;
image = new Image(new FileInputStream(file), 0, 0, true, true);
imageView = new ImageView(image);
}catch (IOException ex) {
ex.printStackTrace();
}
return imageView;
}
尝试创建图片库,但无论我尝试什么,我似乎都无法打开图片文件,
在SceneBuilder上的imageView容器的fx:id在imageView上设置。
答案 0 :(得分:2)
使用javafx打开多选选择文件选择器的代码
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.event.EventHandler;
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 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(
new EventHandler<ActionEvent>() {
@Override
public void handle(final ActionEvent e) {
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
openFile(file);
}
}
});
openMultipleButton.setOnAction(
new EventHandler<ActionEvent>() {
@Override
public void handle(final ActionEvent e) {
List<File> list =
fileChooser.showOpenMultipleDialog(stage);
if (list != null) {
for (File file : list) {
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) {
try {
desktop.open(file);
} catch (IOException ex) {
Logger.getLogger(
FileChooserSample.class.getName()).log(
Level.SEVERE, null, ex
);
}
}
}
了解更多信息http://docs.oracle.com/javafx/2/ui_controls/file-chooser.htm