Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How-to insert Image into Hyperlink in GWT

Is there a way to make a clickable Image in GWT?

like image 684
Antonio Avatar asked Nov 19 '10 12:11

Antonio


5 Answers

What you could do to insert the Image in the Hyperlink:

Hyperlink link = new Hyperlink();
Image image = new Image(someUrl);
...
link.getElement().appendChild(image.getElement());

To make the Image clickable you simply add a ClickHandler to it.

like image 194
z00bs Avatar answered Oct 21 '22 16:10

z00bs


Hyperlink link = new Hyperlink();
Image image = new Image(someUrl);
...
link.getElement().getFirstChild().appendChild(image.getElement());

would be correct. Otherwise the image is added after the hyperlink

like image 22
Florian Avatar answered Oct 21 '22 16:10

Florian


Just attach a ClickHandler to the image:

Image img = new Image(URL);
img.addClickHandler(new ClickHandler() {
  @Override
  public void onClick(ClickEvent ev) {
    // do sth.
  }
};

That's it. The image is clickable. To indicate the clickability to the user just use an appropriate CSS style like cursor:pointer.

like image 29
Makkes Avatar answered Oct 21 '22 14:10

Makkes


    Anchor anchor = new Anchor();
    anchor.getElement().getStyle().setCursor(Cursor.POINTER); 
    anchor.addClickHandler(new ClickHandler() {
          @Override
          public void onClick(ClickEvent ev) {
              Window.Location.assign("http://url.com");
          }
    });
    Image img = new Image("image/path.jpg");
    anchor.getElement().appendChild(img.getElement());

Use Anchor instead of HyperLink because addClickHandler is depreciated in HyperLink. The second line adds the hand pointer to the cursor when you hover the image. The rest is self explanatory I think.

like image 23
Derek Farren Avatar answered Oct 21 '22 16:10

Derek Farren


You can also create a ToggleButton and apply some css styling. Then you have already all the ClickHandler support included.

like image 33
Carlos Tasada Avatar answered Oct 21 '22 15:10

Carlos Tasada