Is there a way to make a clickable Image in GWT?
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.
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
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.
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.
You can also create a ToggleButton
and apply some css styling. Then you have already all the ClickHandler
support included.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With