Display image in table

前端 未结 2 2090
说谎
说谎 2020-12-12 03:22

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\");
    pro         


        
相关标签:
2条回答
  • 2020-12-12 03:41

    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());
    

    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;
    

    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;
    

    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));
    
    0 讨论(0)
  • 2020-12-12 03:46
    1. why are not creating the Image directly from the data new Image(new ByteArrayInputStream(data)) no need to rewrap it our use Swing stuff
    2. I don't see a public Image(Object) constructor in FX8 - why passing it anyways if you are already have an image instance?
    3. you need to set the ImageView on the cell with setGraphic()
    0 讨论(0)
提交回复
热议问题