Java – use clickhandler with elements

I need to add an anchor with a specific clickhandler to the element

How can I solve this problem?

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());

Solution

I haven't tried to implement it this way, but I do and it works

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 it helps

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>