Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT Label like Hyperink

Tags:

java

label

gwt

I want a GWT Label which acts like an hyperlink.

Basically, the label should have an on click method which when clicked opens up a website.I don't want to implement this using an IFrame.

Is there any way i can do this?

Sorry if the question is pathetically easy to solve.

like image 800
krishna Avatar asked Mar 21 '26 10:03

krishna


1 Answers

I'd suggest using Anchor, more specifically, via the Anchor(java.lang.String text) constructor:

Creates an anchor for scripting. The anchor's href is set to javascript:;, based on the expectation that listeners will be added to the anchor.

So, you'll get a good ol' <a> that on click doesn't do anything, but you can add a ClickHandler to it, as such:

Anchor anchor = new Anchor("Click me!"); // At this point clicking it won't do a thing
anchor.addClickHandler(new ClickHandler() {
    @Override
    public void onClick (ClickEvent event){
        Window.open("http://www.example.com/", "_blank", ""); // Or open a PopupPanel
                                                              // or sth similar
    }
});

I'm advising Anchor over Label for accessibility reasons - if it's a link, then it should be an <a>, IMHO. If you really need to use a Label, you can add to it a ClickHandler like shown above.

like image 50
Igor Klimer Avatar answered Mar 22 '26 23:03

Igor Klimer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!