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

前端 未结 3 1046
北恋
北恋 2020-12-28 17:55

How to add the listbox items using UiBinder?

3条回答
  •  余生分开走
    2020-12-28 18:18

    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

      
    

    //JAVA CODE

     @UiField(provided = true)
     ValueListBox requesterType = new ValueListBox(requesterTypeRenderer);
    
     static EnumRenderer requesterTypeRenderer = new EnumRenderer();
    
     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 
      *            an enumeration entry which is to be registered in {@link Translations}
      */
    
    public class EnumRenderer> extends AbstractRenderer {
    
       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;
       }
    
    }
    

提交回复
热议问题