user1782634
user1782634

Reputation: 187

Anchor clickHandler doesn' t fire event

Anchor link = new Anchor("Link");
                link.setStyleName("link");
                link.addClickHandler(new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent event) {
                        Window.alert("mes");
                    }
                });
                relhtml.setHTML(relhtml.getHTML()+link);

Having this snipset of code my HTML shows correctly, and anchor looks like I exactly want to look like, but the event is not fired. Could you help?

Upvotes: 0

Views: 1503

Answers (1)

Riley Lark
Riley Lark

Reputation: 20890

When you call setHTML, you are bypassing all of the GWT architecture and just inserting plain-old HTML into your page. That's why your event handler doesn't work.

The correct way to add a widget to a container is with the add family of methods. For example, you might say RootPanel.get().add(link).

It looks like you are building up an HTML string to insert all at once, but it is OK to just call add on every individual component.

RootPanel root = RootPanel.get();
root.add(link);
root.add(new Button("And this is a button!"));
root.add(whateverOtherWidget);
root.add(new HTML("You can also add arbitrary html with the HTML widget.");

Upvotes: 2

Related Questions