tags:

views:

499

answers:

5

Hello,

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.

A: 

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.

Jason Hall
A: 

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

Ramkumar
+2  A: 

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( ... );
Bluu
A: 

Thank you for your answer.

Even the html text, i am writing dyanamically so i cannot wrap the link in Anchor tag.

Looks like i have to write native method as a work around

Thanks

Ramkumar
+1  A: 

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