What I need is exactly a gwt PushButton in term of behavior with the exception that I need it to have an image in addition to the text.
I use GWT, GWTP and the UiBinder
In my ui.xml file, the following code works, but I can't specify another image if I hover or click the button
<g:SimplePanel height="100%" styleName="{style.button}"
width="100%">
<g:HTMLPanel width="100%" height="100%">
<table align="center">
<tr>
<td>
<g:Image styleName="{style.pane}" resource='{res.Edit}' />
</td>
<td>
<g:Label ui:field="label2" text="Edit Project Edit Project" />
</td>
</tr>
</table>
</g:HTMLPanel>
</g:SimplePanel>
After searching for a solution, I came across this code from here
package com.test.gwtptestproject.client;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Float;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Image;
public class MyButton implements SafeHtml {
private static final long serialVersionUID = 1L;
Image img;
private String text;
private String innerHtml = "";
public void setImage(ImageResource imgr) {
this.img = new Image(imgr);
update();
}
public void setText(String text) {
this.text = text;
update();
}
private void update() {
Element d = DOM.createDiv();
if (img != null) {
Element di = DOM.createDiv();
Style s = di.getStyle();
s.setPaddingLeft(5, Unit.PX);
s.setPaddingRight(5, Unit.PX);
s.setFloat(Float.LEFT);
di.appendChild(img.getElement());
d.appendChild(di);
}
if (text != null) {
Element t = DOM.createDiv();
t.setInnerText(text);
d.appendChild(t);
}
innerHtml = d.getInnerHTML();
}
public MyButton() {
}
@Override
public String asString() {
return innerHtml;
}
}
Which I later used in my ui.xml like this
...
<ui:with field='res' type='com.test.gwtptestproject.resources.ImageResources' />
...
<g:PushButton>
<g:upFace>
<MyButton image="{res.Edit}" text="Edit"></MyButton>
</g:upFace>
</g:PushButton>
My ImageResources package contains the following interface and this package was added to the sources in gwt.xml file
public interface ImageResources extends ClientBundle {
public ImageResources INSTANCE = GWT.create(ImageResources.class);
@Source("Edit.png")
ImageResource Edit();
@Source("Edit_Hover.png")
ImageResource Edit_Hover();
@Source("Edit_Click.png")
ImageResource Edit_Click();
}
When I try to open the designer I keep having the following error and I don't know how to solve it (I get the same error if I try to access the page from a browser)
[ERROR] java.lang.String required, but {res.Edit} returns com.google.gwt.resources.client.ImageResource: <MyButton image='{res.Edit}' text='Edit'> (:110)
[ERROR] Deferred binding failed for 'com.test.gwtptestproject.client.SecondView.Binder'; expect subsequent failures
From what I understand when I write image="{res.Edit}" the application should expect an ImageResource and not a String isn't it ?
*Note : I'm pretty new to web development (school project) so don't presume I might already know some things when you'll answer unless it is obvious