簡體   English   中英

使用文件選擇器在Javafx中打開多個圖像文件

[英]Opening multiple image file in Javafx using file chooser

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的fx:id上的imageView容器在imageView上設置。

使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM