Vaadin:带有 IndexedContainer 的网格中的图像

Vaadin: Images in Grid with IndexedContainer

所以我尝试使用带有以下代码的 IndexedContainer 将图像添加到我的网格中:

    //picture
    String imgURL = (String)ds.child(PHOTO).getValue();//gets the image URL from the DB
    System.out.println(imgURL);
    ExternalResource picture = new ExternalResource(imgURL);
    System.out.println(picture.getURL());
    Image image = new Image(PICTURE, picture);
    image.setHeight("5px");
    item.getItemProperty(PICTURE).setValue(image);

我没有获取图片,而是获取了 Image 对象的 toString()println 都打印出正确的 URL。另请注意,这适用于 Table 但不适用于 Grid。知道为什么吗?

如果要在 Vaadin Grid 列中显示图像,则需要设置 ImageRenderer,请参阅 here 段落 ImageRenderer。

示例:将您的列定义为

grid.addColumn("picture", Resource.class).setRenderer(new ImageRenderer());

然后添加资源作为列值

grid.addRow(new ThemeResource("img/copernicus-128px.jpg"), "Nicolaus Copernicus", 1543);

在你的例子中是 ExternalResource。不需要 Image 组件。