Ramkumar KS
Ramkumar KS

Reputation: 479

GWT Hyperlink getHTML not supporting click event

In my GWT project, we are revamping the design and i am stuck up with an issue.

I my site header, we have a login link which comes in between text like "Already a registered member - Login - Bla bla bla".

I am replacing the "Login" text with new Hyperlink("Login", "").getHTML().

I am getting the link but the click event is not working.

If i add the hyperlink inside a panel like horizontalPanel.add(new Hyperlink("Login", "")), the click event is working fine.

How to solve this issue.


I tried setting the clicklisner both after and before calling getHTML() and it is not working

Upvotes: 0

Views: 1671

Answers (3)

Drejc
Drejc

Reputation: 14286

I would suggest that you just create two widgets one holding the login link and the other the text. Switch the visible flag according to the situation and you will have far less problems and a simpler solution.

Something like that:

loginLink = new Hyperlink("login");
loginLink.addClickHandler(handler);

label = new Label("Already a registered member - ");

horizontalPanel.add(label);
horizontalPanel.add(loginLink);
horizontalPanel.add(new Label(" - Bla bla bla"));

label.setVisible(isLoggedIn());
loginLink.setVisible(!isLoggedIn());

Upvotes: 1

Bluu
Bluu

Reputation: 5565

The String returned by Hyperlink.getHTML() is not a GWT widget, so it does not have click handlers or any special widget abilities associated with it. It is just a String. This is why it works when you add the Hyperlink widget directly to your Panel, like so:

horizontalPanel.add(new Label("Already a registered member - "));
horizontalPanel.add(new Hyperlink("Login", ""));
horizontalPanel.add(new Label(" - Bla bla bla"));

If you favor using widgets rather than their String/HTML representations, their events will work properly.

P.S: If you want an HTML hyperlink without interacting with GWT's history system, you can use an Anchor instead. Say you had this HTML already in your page:

<p>
  Already a registered member -
  <a id="loginLink" href="javascript:void(0);">Login</a>
  - Bla bla bla
</p>

Then you can wrap the existing HTML hyperlink with an Anchor widget and add click handlers to that.

Anchor a = Anchor.wrap(DOM.getElementById("loginLink"));
a.addClickHandler( ... );

Upvotes: 4

Jason Hall
Jason Hall

Reputation: 20930

Are you adding the click handler to the Hyperlink before replacing using getHTML()? If so, I would guess that the code for the click handler isn't included in the HTML being set.

Upvotes: 0

Related Questions