How do I add items to GWT ListBox in Uibinder .ui.xml template ?

23,516

Solution 1

This is a listbox of translations of an enumeration, I suppose this also works for a listbox with string values (version of GWT: 2.1.0)

You only need the renderer for translating the enumeration values.

//UI XML

 <g:ValueListBox ui:field="requesterType"/> 

//JAVA CODE

 @UiField(provided = true)
 ValueListBox<RequesterType> requesterType = new ValueListBox<RequesterType>(requesterTypeRenderer);

 static EnumRenderer<RequesterType> requesterTypeRenderer = new EnumRenderer<RequesterType>();

 public Constructor() {
     requesterTypeRenderer.setEmptyValue(Translations.translateEmptyValue(RequesterType.class));
     requesterType.setAcceptableValues(Arrays.asList(EnumUtil.getRequesterTypes()));
 }

 /**
  * Translates enum entries. Use setEmptyValue() if you want to have a custom empty value. Default empty value is "".
  * 
  * @param <T>
  *            an enumeration entry which is to be registered in {@link Translations}
  */

public class EnumRenderer<T extends Enum<?>> extends AbstractRenderer<T> {

   private String emptyValue = "";

   @Override
   public String render(T object) {
       if (object == null)
           return emptyValue;
       return Translations.translate(object);
   }

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

}

Solution 2

It is possible since february 2011 version:

http://code.google.com/p/google-web-toolkit/issues/detail?id=4654

Following this patch you are now able to add items following this syntax:

<g:ListBox>
  <g:item value='1'>
    first item
  </g:item>
  <g:item value='2'>
    second item
  </g:item>
</g:ListBox>

Solution 3

GWT ValueListbox otherwise know as a ComboBox or Dropdown component. Another example that also demonstrates populating the list.

UiBinder...

<g:ValueListBox ui:field="subCategory"/> 

Editor...

@UiField(provided = true)
ValueListBox<String> subCategory = new ValueListBox<String>(
  new Renderer<String>() {

    @Override
    public String render(String object) {
      String s = "Cats";
      if (object != null) {
        s = object.toString();
      }
      return s;
    }

    @Override
    public void render(String object, Appendable appendable)
        throws IOException {
      render(object);
    }

});

Constructor...

List<String> values = new ArrayList<String>();
values.add("Animal Shelters and Rescues");
values.add("Birds");
values.add("Cats");
values.add("Dogs");
values.add("Other Pets");
values.add("Rabbits");
subCategory.setAcceptableValues(values);
Share:
23,516

Related videos on Youtube

Kasturi
Author by

Kasturi

Software Engineer Amazon

Updated on July 09, 2022

Comments

  • Kasturi
    Kasturi almost 2 years

    How to add the listbox items using UiBinder?

    • aem
      aem almost 14 years
      code.google.com/webtoolkit/doc/latest/… shows how to add ListBox items from the Java file, though I presume you're asking if it's possible to specify them in the UiBinder template.
    • Kasturi
      Kasturi almost 14 years
      yup.... i'm asking if we can give the items in the uibinder itself
  • expert
    expert about 12 years
    Gunnie, how would you marry your solution with declarative internationalization described here ?