如何使用UiBinder添加列表框项?
这是枚举的翻译列表框,我想这也适用于具有字符串值的列表框(GWT版本:2.1.0)
您只需要渲染器来翻译枚举值。
// 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; } }