1

I am trying to insert an image into table view in JavafX. Here is how I set up my table view:

TableColumn prodImageCol = new TableColumn("IMAGES");
    prodImageCol.setCellValueFactory(new PropertyValueFactory<Product, Image>("prodImage"));
    prodImageCol.setMinWidth(100);
    // setting cell factory for product image        
    prodImageCol.setCellFactory(new Callback<TableColumn<Product,Image>,TableCell<Product,Image>>(){        
        @Override
        public TableCell<Product,Image> call(TableColumn<Product,Image> param) {                
            TableCell<Product,Image> cell = new TableCell<Product,Image>(){
                    public void updateItem(Product item, boolean empty) {                        
                    if(item!=null){                            
                        ImageView imageview = new ImageView();
                        imageview.setFitHeight(50);
                        imageview.setFitWidth(50);
                        imageview.setImage(new Image(product.getImage()));
                    }
                }
            };
            return cell;
        }

    });        

 viewProduct.setEditable(false);
 viewProduct.getColumns().addAll(prodImageCol, prodIDCol, prodNameCol, prodDescCol, prodPriceCol, col_action);
 viewProduct.getItems().setAll(product.populateProductTable(category));

 private SimpleObjectProperty prodImage;

 public void setprodImage(Image value) {
    prodImageProperty().set(value);
}

public Object getprodImage() {
    return prodImageProperty().get();
}

public SimpleObjectProperty prodImageProperty() {
    if (prodImage == null) {
        prodImage = new SimpleObjectProperty(this, "prodImage");
    }
    return prodImage;
}

And this is how I retrieve the image from database:

 Blob blob = rs.getBlob("productImage");
 byte[] data = blob.getBytes(1, (int) blob.length());
 bufferedImg = ImageIO.read(new ByteArrayInputStream(data));
 image = SwingFXUtils.toFXImage(bufferedImg, null);

However I am getting error at the setting up of table view: imageview.setImage(new Image(product.getImage())); The error message as:

no suitable constructor found for Image(Image)
constructor Image.Image(String,InputStream,double,double,boolean,boolean,boolean) is not applicable
  (actual and formal argument lists differ in length)
constructor Image.Image(int,int) is not applicable
  (actual and formal argument lists differ in length)
constructor Image.Image(InputStream,double,double,boolean,boolean) is not applicable
  (actual and formal argument lists differ in length)
constructor Image.Image(InputStream) is not applicable
  (actual argument Image cannot be converted to InputStream by method invocation conversion)
constructor Image.Image(String,double,double,boolean,boolean,boolean) is not applicable
  (actual and formal argument lists differ in length)
constructor Image.Image(String,double,double,boolean,boolean) is not applicab...

I did managed to retrieve and display an image inside an image view but however, I can't display it in table column. Any help would be appreciated. Thanks in advance.

4

2 回答 2

3

导致异常的问题是您的方法product.getImage()返回一个javafx.scene.Image. 在这一点上没有必要做任何其他事情:你有一个图像,所以使用它(在你尝试构建之前new Image(Image)- 这甚至是不可能的)。这是您想要使用的:

imageview.setImage(product.getImage());

您的第二个问题是,当您在ImageView每次更新单元格时创建一个时,您并没有对它做任何事情。这是您的原始代码:

        TableCell<Product,Image> cell = new TableCell<Product,Image>(){
                public void updateItem(Product item, boolean empty) {                        
                if(item!=null){                            
                    ImageView imageview = new ImageView();
                    imageview.setFitHeight(50);
                    imageview.setFitWidth(50);
                    imageview.setImage(new Image(product.getImage()));
                }
            }
        };
        return cell;

就像@tomsontom 建议的那样,我建议使用setGraphic(Node)将您附加ImageViewTableCell. 所以你最终可能会得到这样的结果:

        //Set up the ImageView
        final ImageView imageview = new ImageView();
        imageview.setFitHeight(50);
        imageview.setFitWidth(50);

        //Set up the Table
        TableCell<Product,Image> cell = new TableCell<Product,Image>(){
                public void updateItem(Product item, boolean empty) {                        
                if(item!=null){
                    imageview.setImage(product.getImage());  //Change suggested earlier
                }
            }
        };

        // Attach the imageview to the cell
        cell.setGraphic(imageview) 
        return cell;

@tomsontom 提出的第一点是您创建图像的方法有点迂回。当然,它似乎工作......但有一个更简单的方法。最初您使用的是:

 bufferedImg = ImageIO.read(new ByteArrayInputStream(data));
 image = SwingFXUtils.toFXImage(bufferedImg, null);

但更好的方法是用以下方式切换这些行:

 image = new Image(new ByteArrayInputStream(data));
于 2013-07-17T14:49:00.243 回答
2
  1. 为什么不直接从数据创建图像 new Image(new ByteArrayInputStream(data)) 不需要重新包装我们使用 Swing 的东西
  2. 我在 FX8 中没有看到公共 Image(Object) 构造函数 - 如果您已经有图像实例,为什么还要传递它?
  3. 您需要使用 setGraphic() 在单元格上设置 ImageView
于 2013-07-17T11:00:34.300 回答