1

Is there a way to add image or icon inside a GWT TextBox widget?

EDIT: The image is required to have a ClickHandler.

2 Answers 2

3

If you are only interested in visually adding an icon , you can add it using css such as :

background-image:url('icon.png');
background-repeat:no-repeat;

UPDATE :

If you need to add events to image, you can bind an image and a textbox in a horizontal panel as in @Sandro Munda's answer. Also another method is to use an absolute panel and css to make the image and the textbox overlap as such :

public class TextBoxWithImage extends Composite {
    public TextBoxWithImage() {
        AbsolutePanel p = new AbsolutePanel();
        p.add(new TextBox());       
        Image image = new Image("images/down.png");
        image.getElement().getStyle().setMarginLeft(-20, Unit.PX);
        p.add(image);       
        image.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                Window.alert("Clicked!");
            }
        });
        initWidget(p);
    }
}
1
  • However I need to add ClickHandler to the image.
    – quarks
    Sep 15, 2011 at 0:55
1

Not directly.

You can extend an HorizontalPanel and create your new widget like the ValueSpinner class does (from the Gwt Mosaic project).

ValueSpinner.java

As you can see, the ValueSpinner joins a TextBox and an Image inside a HorizontalPanel to create the widget.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.