how to add an image to a cell in cellTable in GWT
Asked Answered
H

3

5

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.

Hailee answered 5/5, 2011 at 19:22 Comment(2)
I cannot see a problem with this. Do you get a completely blank cell of a missing image icon?Glop
where do you have the file "contact.jpg"? It should be directly placed under /war. It will not work otherwise.Holarctic
G
16

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();
    }
  };
Gearard answered 19/7, 2011 at 13:39 Comment(3)
agreed! ImageResourceCell would help to implement the Image inside a celltable. However, this looks more of a static rather a dynamic implementation. This forces the user to associate each image with a method in the interface (Resources). However, in most cases the location of the image is obtained dynamically. For instance, I have a project with 1000images and want to populate them in a celltable. I can't create 1000 methods each corresponding to an image. Is there a generic solution for this?Holarctic
@Ashok, you can always create your own class that implements ImageResource and then use instances of that class to populate the cells. Something like this: gist.github.com/1124648Polypeptide
Hey guys, there is a problem with this type: "we can't scale the image". There are different solutions to this: 1. pre-image loader 2. FITImage etc., However one simple solution (markmail.org/message/…) provided works better. Its simple: using a htmlcell instead of imagecell and assigning an image link to that htmlcell. It scales perfectly fine as given width and height. thought of sharing with you all.Holarctic
T
9
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, "");
Territorialize answered 17/1, 2013 at 10:17 Comment(0)
A
5

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

Aerostatic answered 28/12, 2011 at 11:19 Comment(1)
don't forget also the "return" keyword in the getValue methodStatutable

© 2022 - 2024 — McMap. All rights reserved.