belgoros
belgoros

Reputation: 3908

GWT: Add an image to Anchor

How is it possibe to add an image to an Anchor element ? I tried a solution found here but still have the target URL text displayed alongside the image. Any ideas ? Thank you. I use GWT 2.5.1.

Upvotes: 0

Views: 886

Answers (3)

Venkata Naresh Babu
Venkata Naresh Babu

Reputation: 367

Write this code in "sample.ui.xml" in client package

<g:FlowPanel addStyleNames="col-lg-offset-2 col-lg-10">
    <g:Anchor ui:field="myAnchor"></g:Anchor>
</g:FlowPanel>

write this code in "sample.java" in client package

//declare the variable

@UiField
Anchor myAnchor;

//inside the contractor

myAnchor.setHref("http://google.com");
myAnchor.setText("Google");
myAnchor.setHTML("<img src='google.png'>"); // place google.png file in GWT project war folder (war/google.png) 
myAnchor.setTitle("Google");
myAnchor.setVisible(true);

this will give the image with anchor tag(hyper link) GWT project

Upvotes: 0

Thomas Broyer
Thomas Broyer

Reputation: 64541

new Anchor("<img src=\"path/to/your/image.png\">", true, "http://example.com", "_blank");

Upvotes: 1

belgoros
belgoros

Reputation: 3908

The solution that worked for me and that opened a link in a new tab:

Image image = new Image("path/to/your/image.png");
Anchor anchor = new Anchor("", "http://your-fancy-url.com", "_blank");
anchor.getElement().getStyle().setCursor(Style.Cursor.POINTER);
anchor.getElement().appendChild(mylo.getImage().getElement());

Thank you to Sebastian and peotest help.

Upvotes: 1

Related Questions