destiny
destiny

Reputation: 1862

Create own Handler for subwidgets in GWT

i want to create a handler for a superwidget which handles onclick events for subwidgets. Is there a option to do this?

For example: I want to register a Clickhandler to a absoultePanel. The absolutePanel contains different Image-Widgets.

Of cause, i could register the handler for each image, but i need the same handler for each image because i need some global informations.

i thought i can do something like this: if (event.getSource() instanceof Image) but it works not for me

Greetz, destiny

Upvotes: 1

Views: 270

Answers (2)

Danny Kirchmeier
Danny Kirchmeier

Reputation: 1134

In response to your attempt, event.getSource() will return the widget the handler was attached to. In your case, the absolute panel.

That said, If you want to attach the same handler to all of your images, there is nothing stopping you from doing this:

ArrayList<Image> listOfImages = ...
ClickHandler ch = new MyImageClickHandler();
for (Image img : listOfImages){
   img.addClickHandler(ch);
}

That said, if you still want to add the click handler to the image containment panel, consider this:

Panel imgContainer = ...
ClickHandler ch = new ClickHandler(){
   public void onClick(ClickEvent event){
      Element e = Element.as(event.getNativeEvent().getEventTarget());
      if("img".equalsIgnoreCase(e.getTagName())){
         ImageElement img = ImageElement.as(e)
         //Clicked on image. Do stuff.
      }
   }
}

// If your panel implements HasClickHandlers
imgContainer.addClickHandler(ch);

// Otherwise, use this
imgContainer.addDomHandler(ch, ClickEvent.getType());

Upvotes: 1

idle
idle

Reputation: 1137

If I understand the question correctly, then FocusPanel seems like what you are looking for

Upvotes: 0

Related Questions