I want to create a button with an icon and text where the icon is above the text.
Looking for information, I found an answer from Juri .
With it, I created my own SquareButton class, as shown below:
import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.resources.client.ImageResource; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Element; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.Image; public class SquareButton extends Button { private String text; public SquareButton() { super(); } public SquareButton(String text, ClickHandler clickHandler, ImageResource imageResource) { super(text, clickHandler); setResource(imageResource); setPixelSize(60, 60); } public void setResource(ImageResource imageResource){ Image img = new Image(imageResource); String definedStyles = img.getElement().getAttribute("style"); img.getElement().setAttribute("style", definedStyles + "; vertical-align:top;"); DOM.insertBefore(getElement(), img.getElement(), DOM.getFirstChild(getElement())); } @Override public void setText(String text) { this.text = text; Element span = DOM.createElement("span"); span.setInnerText(text); span.setAttribute("style", "vertical-align:bottom; text-align: center;"); DOM.insertChild(getElement(), span, 0); } @Override public String getText() { return this.text; } }
Unfortunately, it does not work as I expect, and we display the text above the image.
Could you give me a hand?
source share