将ListView(图像)添加到GridPane中?

时间:2015-12-08 10:55:52

标签: java listview javafx imageview gridpane

尝试以缩略图形式显示图像到gridpane中的listview

 ObservableList<File> imageFiles = FXCollections.observableArrayList();
 List<File> list = fileChooser.showOpenMultipleDialog(null);

    if(list != null){
        for(File file : list){
            //Image image = new Image(file.toURI().toString());
            final Image image = new Image(new FileInputStream(file), 150, 150, true, false);
            ImageView imageView = new ImageView();
            imageView = createImageView(image);

            if(check1.isSelected()){


                VBox vl = new VBox();
                vl.getChildren().addAll(imageView);

                ListView<File> imageFilesList = new ListView<>(imageFiles);
                imageFilesList.setCellFactory(listview -> new ListCell<File>(){
                    private final ImageView imageView = new ImageView();
                    {
                        imageView.setFitHeight(80);
                        imageView.setFitWidth(160);
                        imageView.setPreserveRatio(true);
                    }

                    @Override
                    public void updateItem(File file, boolean empty) {
                        super.updateItem(file, empty);
                        if (empty) {
                            setText(null);
                            setGraphic(null);
                        } else {
                            setText(file.getAbsolutePath().toString());
                            imageView.setImage(new Image(file.getAbsolutePath().toString(), true));
                            setGraphic(imageView);
                        }
                    }
                    });

                grid.add(imageFilesList, imageCol, imageRow);

我已尽力尝试,这是我的输出,我错过了什么?为什么GridPane仍然以缩略图大小加载它?

image

1 个答案:

答案 0 :(得分:0)

没有显示图片,因为

1)网格视图包含ListView imageFilesList,此列表视图包含imageFiles。但是您没有将任何图像文件添加到imageFiles 2)通过fileChooser选择的图像文件已添加到VBox vl。但是,此vbox未添加到gridpane。