【问题标题】:Vaadin: Images in Grid with IndexedContainerVaadin:带有 IndexedContainer 的网格中的图像
【发布时间】:2016-06-09 14:19:09
【问题描述】:

所以我尝试使用带有以下代码的 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。另请注意,这适用于表格,但不适用于网格。知道为什么吗?

【问题讨论】:

  • 哪个 println 打印了正确的 URL? PICTURE 有什么价值?
  • 两者都有。这是网格单元格中显示的内容:com.vaadin.ui.Image@1e37c05d
  • 答案有帮助吗?请提供反馈。

标签: vaadin vaadin7


【解决方案1】:

如果要在 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 组件。

【讨论】:

  • 唉,我使用的是 IndexedContainer,所以这并不适用。
  • 嗯,这只是一个例子。你的IndexedContainer 需要一个类型为Resource 的列,例如ThemeResource 然后将渲染器设置为grid.getColumn("yourColumn").setRenderer(new ImageRenderer())
  • 我都做到了。正如我所说,相同的代码适用于 Table。我继续前进并决定改用 Table。也许问题是我声明该列具有Image 类型而不是Resource
  • 这个答案是正确的。问题是我尝试将属性(列)的类型设置为ExternalResource。使用Resource.class 并将属性值传递给ExternalResourceThemeResource 就可以了。谢谢。
  • 您可以将其标记为答案,请参阅here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-27
  • 1970-01-01
  • 2014-06-02
  • 1970-01-01
  • 2017-12-23
  • 1970-01-01
  • 2019-12-28
相关资源
最近更新 更多