sherry
sherry

Reputation: 1859

how to add an image to a cell in cellTable in GWT

I want to add an image in a cell in the CellTable. After reading the documentation, this is what I did,

Column<Contact, String> imageColumn = new Column<Contact, String>(new ImageCell()) {
    @Override
    public String getValue(Contact object) {
        return "contact.jpg";
    }
  };
table.addColumn(imageColumn, "");

Well, there's an empty column in the table now and no image in it. Am I doing something wrong here? Any suggestion is appreciated. Thank you.

Upvotes: 5

Views: 14035

Answers (3)

Ionuț G. Stan
Ionuț G. Stan

Reputation: 179119

Use an ImageResourceCell:

interface Resources extends ClientBundle {
  @Source("image-path.png")
  ImageResource getImageResource();
}

Resources resources = GWT.create(Resources.class);

Column<Contact, ImageResource> imageColumn =
  new Column<Contact, ImageResource>(new ImageResourceCell()) {
    @Override
    public ImageResource getValue(Contact object) {
      return resources.getImageResource();
    }
  };

Upvotes: 16

Jay
Jay

Reputation: 691

Column<Contact, String> imageColumn = 
    new Column<Contact, String>(
        new ClickableTextCell() 
        {
            public void render(Context context, 
                               SafeHtml value, 
                               SafeHtmlBuilder sb)
            {
                sb.appendHtmlConstant("<img width=\"20\" src=\"images/" 
                                       + value.asString() + "\">");
            }
        })
        {
            @Override
            public String getValue(Contact object) {
                return "contact.jpg";
            }
        };
        table.addColumn(imageColumn, "");

Upvotes: 9

gzorzi
gzorzi

Reputation: 51

I suppose there's a error in the Ionuț G. Stan post

i suppose is

Column<Contact, ImageResource> imageColumn =
  new Column<Contact, ImageResource>(new ImageResourceCell()) {
    @Override
    public ImageResource getValue(Contact object) {
      resources.getImageResource();
    }
  };

so with public ImageResource getValue and not public String getValue

Upvotes: 5

Related Questions