3

I need to add an Anchor with a specific ClickHandler into an Element. But the onClick(...) method of my Anchor is never called.

How can I fix that?

Element th = DOM.createTH();
Anchor link = new Anchor();
link.setText("my link");
link.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
      Window.alert("Clicked!");
    }
});
th.appendChild(link.getElement());
Jama A.
  • 15,680
  • 10
  • 55
  • 88
user1180339
  • 319
  • 1
  • 6
  • 16

1 Answers1

7

I have not tried implementing it in such way but, I do in this way and it is working properly.

final Element link = DOM.createAnchor();
final Element th = DOM.createTH();
link.setInnerText("my link");
link.setAttribute("style", "cursor:pointer;");
DOM.sinkEvents(link, Event.ONCLICK);
DOM.setEventListener(link, new EventListener() {
      public void onBrowserEvent(Event event) {
          Window.alert("Clicked!");     
      }
});
th.appendChild(link);

I think this helps.

Jama A.
  • 15,680
  • 10
  • 55
  • 88