Using FocusWidget.addClickHandler for GWT 2.0 Hyperlink (ClickHandler deprecated)

448 views
Skip to first unread message

Tapas Adhikary

unread,
Feb 19, 2010, 2:18:48 PM2/19/10
to Google Web Toolkit
I have upgraded my project to GWT 2.0, and my hyperlinks keeps getting
the wornings for using the deprecated ClickHandler.

The JavaDoc says to use FocusWidget.addClickHandle. Why does the
Hyperlink need to use this FocusWidget? How should I use it ? Why can
a Hyperlink not use a ClickHandler but a GWT Button and Anchor can?
What is the logic behind it ?

I'm not sure how to use it? Does anyone have an example? Please share.

Thanks,
-Tapas

Zak

unread,
Feb 20, 2010, 12:48:36 PM2/20/10
to Google Web Toolkit
Hi Tapas-

Check out this issue for the reasoning behind the deprecation of
Hyperlink.addClickHandler:

http://code.google.com/p/google-web-toolkit/issues/detail?id=1960

from tamplinjohn's comment on that thread:

"I also think that allowing click listeners/handlers on Hyperlink is
part of the
reason for the confusion -- if its goal is to manipulate the history
state, there
isn't any reason for adding a listener on the click, but you should
instead add a
history listener so that function works the same way whether activated
by a click, a
back button, or loading from a bookmark. If for some reason you do
really need a
click handler, then it can still be done by using Anchor and calling
History.newItem
from its listener."

Tapas Adhikary

unread,
Feb 20, 2010, 10:15:48 PM2/20/10
to google-we...@googlegroups.com
So does it mean , I need to change the HyperLink to anchor wherever I need a link functionality but doesn't need a history support ? Can anybody provide some suggestion / example code ?

Thanks,
-Tapas

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.


Zak

unread,
Feb 21, 2010, 6:59:32 PM2/21/10
to Google Web Toolkit
Yes, you are correct. If you had this before:

Hyperlink link = new Hyperlink("text");
link.addClickHandler(...);

you should change it to this:

Anchor link = new Anchor("text");
link.addClickHandler(...);

However, Hyperlink is block-level whereas Anchor is inline. Anchor is
actually more analogous to InlineHyperlink (subclass of Hyperlink).
You should keep that in mind insofar as keeping your layout consistent
after making the switch.


On Feb 20, 10:15 pm, Tapas Adhikary <tapas4...@gmail.com> wrote:
> So does it mean , I need to change the HyperLink to anchor wherever I need a
> link functionality but doesn't need a history support ? Can anybody provide
> some suggestion / example code ?
>
> Thanks,
> -Tapas
>

> > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsu...@googlegroups.com>

javaunixsolaris

unread,
Feb 25, 2010, 4:54:57 PM2/25/10
to Google Web Toolkit
Thanks Zak Anchor works great. Do you also have an example of if you
want to:

Hyperlink link = new Hyperlink("text");
link.addClickHandler(...);

to

The new History way to do it?

Zak

unread,
Feb 25, 2010, 7:43:04 PM2/25/10
to Google Web Toolkit
Hi javaunixsolaris-

I'm not exactly sure what your question is. The "history way" is not
new in GWT 2.0. Read about it here:
http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsHistory.html

If you want to know how to use an <a> element that handles clicks AND
changes history, I believe the @deprecated comment for
Hyperlink.addClickHandler() implies you should extend FocusWidget and
use its addClickHandler(). Something like this:

public class MyHyperlink extends FocusWidget {
public MyHyperlink(String text) {
super(DOM.createAnchor());
// set the text
}
// you should cherry-pick the methods implemented in Hyperlink
// you want, like setTargetHistoryToken(), and impl them here
}
MyHyperlink link = new MyHyperlink("text");
link.addClickHandler(...);

However, I have not tried or tested this, so I'm not sure that's the
best way. A specific use case might help :).

javaunixsolaris

unread,
Mar 3, 2010, 7:22:51 PM3/3/10
to Google Web Toolkit
Thanks Zak, that's exactly what I meant by the "history way". Because
I thought by deprecating Hyperlink.addClickHandler that was GWT's way
of saying, "if you use the Hyperlink class make sure to use history
tokens!"... I'd like to know the actual reason it became deprecated?
Mainly because Anchor doesn't work for me.

These two ways of handling clicks give me different behavior:

Hyperlink link = new Hyperlink("text");
link.addClickHandler(...);

=NOT the same as:


Anchor link = new Anchor("text");
link.addClickHandler(...);

Anchors actually crash the app!


On Feb 25, 5:43 pm, Zak <zakn...@gmail.com> wrote:
> Hi javaunixsolaris-
>
> I'm not exactly sure what your question is. The "history way" is not

> new in GWT 2.0. Read about it here:http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsHist...

Thomas Broyer

unread,
Mar 4, 2010, 11:10:28 AM3/4/10
to Google Web Toolkit

On Mar 4, 1:22 am, javaunixsolaris <lpah...@gmail.com> wrote:
> Thanks Zak, that's exactly what I meant by the "history way".  Because
> I thought by deprecating Hyperlink.addClickHandler that was GWT's way
> of saying, "if you use the Hyperlink class make sure to use history
> tokens!"...  I'd like to know the actual reason it became deprecated?
> Mainly because Anchor doesn't work for me.
>
> These two ways of handling clicks give me different behavior:
>
> Hyperlink link = new Hyperlink("text");
> link.addClickHandler(...);
> =NOT the same as:
> Anchor link = new Anchor("text");
> link.addClickHandler(...);
>
> Anchors actually crash the app!

Can you clarify "crash the app", and what is your ClickHandler doing?

1. if you want a "Hyperlink with ClickHandler", use an Anchor, set its
Href to #token (token being your history token), and in the
ClickHandler make sure you preventDefault() on the event and call
History.newItem() (this is what Hyperlink actually does)
2. if you want a link to another web page, well, I don't see a reason
it would "crash"...
3. if you just want some kind of "button looking like link", then do
not (ab)use an Anchor, use a Label or PushButton instead and style it
as you want it to look like.

Message has been deleted

javaunixsolaris

unread,
Mar 4, 2010, 7:49:55 PM3/4/10
to Google Web Toolkit
I just noticed something, these two constructors aren't the same,
that's probably my problem sorry:
Hyperlink myLogoutHyperlink = new Hyperlink("Logout", "");
Anchor myLogoutHyperlink = new Anchor("Logout", "");
Reply all
Reply to author
Forward
0 new messages