tags:

views:

817

answers:

2

I want to click on an image and therefore want to register (e.g.) a ClickHandler. The image I get from a ClientResource. This works so far to set the image into a table cell:

MyResources.INSTANCE.css().ensureInjected();
Image colorImage = new Image( MyResources.INSTANCE.colorImage() );
Element colorImageElement = colorImage.getElement();

colorImage.addClickHandler(new ClickHandler() {
 public void onClick(ClickEvent event) {
  System.out.println( event );
 }
} );

TableElement table = Document.get().createTableElement();
TableRowElement headRow = table.insertRow(-1);
headRow.insertCell(-1).appendChild( colorImageElement );

RootPanel.get().getElement().appendChild( table );

How can I add a listener to the icon? I tried ClickHandler and to put the image on a PushButton and get the Element from this PushButton but all don't work.

But mind, if I add the widget (Image is a Widget) to a panel it works!

RootPanel.get().add( colorImage );

But I am not working with widgets here but with the Element. So the handler disappears and that's the point I don't get how to preserve this added handler information.

In the end I would like to build a table with different rows where I can click on the icon I get a popup menu and thereby change the colour of the row.

+3  A: 

You should be able to just add a ClickHandler (or a MouseDownHandler if that fits your needs better).

Like this:

colorImage.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                // Do something....
            }
        });
Fredrik
I tried this before but this does not work. I tried different handlers, FocusWidget, ... no reaction.
Paulus
@Paulus: In a project I am currently working on I do exactly this but with a MouseDownHandler (addMouseDownHandler) and it works great, click handlers should work equally good. Please post some more code where you actually try it out but fail. Maybe it is just a stupid typo or something.
Fredrik
Thanks for your help. I updated my question with a demo suited for onModuleLoad() that shows that this solution don't work.
Paulus
@Paulus: It could be that you are just using the element (why? that is not how GWT should be used when building a UI). I guess if you bypass everything GWT does for you you will just have have to add handlers and scripts to the element yourself as well.
Fredrik
A: 

did you tried to add image.sinkEvents( Event.ONCLICK | Event.MOUSEEVENTS )?

zmila