开发者

setting a default text to a GWT ListBox

开发者 https://www.devze.com 2023-02-22 00:18 出处:网络
I am trying to create a ListBox using GWT. I am using UiBinder to cr开发者_StackOverflow社区eate the field.

I am trying to create a ListBox using GWT. I am using UiBinder to cr开发者_StackOverflow社区eate the field.

I would like to set a default text on the list box and when a user clicks on the box, it should show me the list items. Once again, if user has not selected any option, it should show me the default text again.

Any way to do this either using Uibinder or some ListBox methods?


If I understand correctly you want a value to show but when the user clicks on the list it disappears and shows you the list items? As far as I know there is no option to that natively.

What you can do is add the first item to hold your default value. You can do this grammatically by using addItem in code or using:

<g:Listbox> 
    <g:item value="-1">Default text</g:item>
</g:Listbox>

works with gwt 2.1+

The value can still be selected. You can choose to ignore it or add an attribute "disabled" with value "disabled" to the option element:

listbox.getElement().getFirstChildElement().setAttribute("disabled" ,"disabled" )

hope it helps a bit :)


You can also use a renderer to control what is shown if 'Null' is selected.

(Inspired by: How do I add items to GWT ListBox in Uibinder .ui.xml template ?)

private class SimpleRenderer implements Renderer<T>{
    private String emptyValue = "Select a value";

    @Override
    public String render(T val) {
        if(val == null) {
            return emptyValue;
        }
        return val.toString();
    }

    @Override
    public void render(T val, Appendable appendable) throws IOException {
        appendable.append(render(val));
    }

    public void setEmptyValue(String emptyValue) {
        this.emptyValue = emptyValue;
    }


}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号