在表格中显示图像 [英] Display image in table
问题描述
我正在尝试将图像插入JavafX中的表视图中。以下是我设置表视图的方法:
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);
但是我在设置表视图时遇到错误:imageview.setImage(new Image(product .getImage()));
错误消息:
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.
推荐答案
导致异常的问题是您的方法 product.getImage()
返回 javafx.scene.Image
。此时无需做任何其他事情:你有一个图像,所以使用它(在你尝试构建新图像(图像)
之前 - 这甚至都不可能) )。这就是你想要使用的:
The problem that's causing the exception is that your method product.getImage()
is returning an javafx.scene.Image
. There's no need to do anything else at this point: You have an image, so use it (before you were trying to construct new Image(Image)
- which is not even possible). This is what you want to be using:
imageview.setImage(product.getImage());
您的第二个问题是,当您创建 ImageView $时c $ c>每次更新单元格时,你都没有做任何事情。这是您的原始代码:
Your second problem is that while you're creating an ImageView
every time you update the cell, you're not doing anything with it. Here's your original code:
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)附加 ImageView
到 TableCell
。所以你最终会得到这样的结果:
Like @tomsontom suggested, I'd recommend using setGraphic(Node) to attach your ImageView
to the TableCell
. So you might end up with something like this:
//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的第一点是你创建图像的方法有点迂回。当然,它似乎有效...但有一种更简单的方法。最初你使用的是:
The first point @tomsontom was making is that your method of creating an Image is a little roundabout. Sure, it seems to work... but there's a simpler way. Originally you were using:
bufferedImg = ImageIO.read(new ByteArrayInputStream(data));
image = SwingFXUtils.toFXImage(bufferedImg, null);
但更好的方法是用以下方式切换这些行:
But a better way of doing it would be switching those lines with:
image = new Image(new ByteArrayInputStream(data));
这篇关于在表格中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!