开发者

How-to insert Image into Hyperlink in GWT

开发者 https://www.devze.com 2023-01-26 10:18 出处:网络
Is there a way to make a clickable 开发者_开发知识库Image in GWT?What you could do to insert the Image in the Hyperlink:

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.


From a general point of view.. We can write an onclick event for the image and a css cursor: pointer (optional) to give it a hyperlink feel.

0

精彩评论

暂无评论...
验证码 换一张
取 消