0

I want to create a button with an icon and text, where icon is above text.

Looking for information I've found this answer from Juri.

With it I've created my own SquareButton class like 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;

/**
 * Square Button class
 * @author julio.palma.vazquez
 *
 */
public class SquareButton extends Button {

    /** Text to show. */
    private String text;

    /**
     * Constructor.
     */
    public SquareButton() {
        super();
    }

    public SquareButton(String text, ClickHandler clickHandler, ImageResource imageResource) {
        super(text, clickHandler);
        setResource(imageResource);
        setPixelSize(60, 60);
    }

    /**
     * Set image resource.
     * @param imageResource image resource
     */
    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()));
    }

    /**
     * Set text.
     * @param text text to show
     */
    @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);
   }

   /**
    * Get text.
    * @return text to show
    */
   @Override
   public String getText() {
       return this.text;
   }
}

Unfortunatelly it doesn't work as I expect and output text above image.

Could you please give me a hand?

Community
  • 1
  • 1
restalion
  • 3
  • 1
  • 2

3 Answers3

5

You can set HTML in Button. You can try following:

Button button = new Button();
String html = "<div><center><img src = '"+GWT.getModuleBaseURL()+"/images/img1.png' height = '10px' width = '10px'></img></center><label>Text</label></br></div>";
button.setHTML(html);

Give proper size to your button as well as image.

JankiPanwala
  • 584
  • 1
  • 4
  • 20
  • @restalion, if the answer is helpful to you, then accept it. Also upvote it, if it's a nice answer. – RAS Jul 11 '12 at 12:13
0

You can see SquareButton class below, thanks for your help.

import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Button;

/**
 * Square Button class
 * @author julio.palma.vazquez
 *
 */
public class SquareButton extends Button {

    public SquareButton(String text, ClickHandler clickHandler, ImageResource imageResource) {
        super();
        String html = "<div><center><img src = '" +     imageResource.getSafeUri().asString() + "' height = '32px' width = '32px'></img></center><label>" + text + "</label></br></div>";
        addClickHandler(clickHandler);
        setHTML(html);
        setPixelSize(60, 60);
    }
}
restalion
  • 3
  • 1
  • 2
0

You don't need a custom button. You can do this with a GWT Button, ImageResource, and CSS. The trick is to set the padding properly. For example,

Button btnHelp=new Button("Help");
btnHelp.setStyleName("buttonImageText");
// Separate style for each image resource
btnHelp.addStyleName("bgImageHelp");

In your stylesheet: (Note: padding-top is at least as large as your image height.)

.buttonImageText{
   background-repeat: no-repeat;
   background-position: 50% 0%;
   background-size: 20px 20px;
   background-color: transparent;
   border: none;
   margin: 0 15px;
   padding: 20px 0 0 0;
   text-align: center;
}

.bgImageHelp {
   background-image: helpUrl;
}

And you've also defined in your CSS file:

@url helpUrl help;

Finally, in your Resources class you have defined:

@Source("resources/images/help.png");
ImageResource help();
Alicia
  • 144
  • 1
  • 9