quarks
quarks

Reputation: 35346

How to add image or icon inside a GWT TextBox widget?

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

EDIT: The image is required to have a ClickHandler.

Upvotes: 1

Views: 6439

Answers (2)

pistolPanties
pistolPanties

Reputation: 1890

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);
    }
}

Upvotes: 3

Sandro Munda
Sandro Munda

Reputation: 41078

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.

Upvotes: 1

Related Questions