Search code examples
gwtlistboxuibinder

setting a default text to a GWT ListBox


I am trying to create a ListBox using GWT. I am using UiBinder to create 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?


Solution

  • 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 :)