Oops Null Pointer

Java programming related

GXT Enums, ComboBoxes and i18n

If you want to have a ComboBox that uses enums with i18n you can use a SimpleComboBox and use the enums toString() method to return the GWT i18n constants.

As I posted on the GXT forums, I don’t want to use toString() as I want the enum to still work on the server side (say during debugging).

Instead I have a getLabel() method and want the GXT ComboBox to call that. Here’s the code:

It does the following:

  • Convert the value in the ComboBox with a property editor to call getLabel()
  • Convert the values in the list with a ModelProcessor to call getLabel() and Template
  • The Enum

    public enum Condition {
      public String getLabel()
          // get i18n with a GWT create call save via a ConstantsWithLookup

    The ComboBox that uses Condition enum (with a getLabel() method for i18n)

    final SimpleComboBox<Condition> combo = new SimpleComboBox<Condition>();

    The standard enum field binding:

    FieldBinding binding = new FieldBinding(combo, "condition") 
        protected Object onConvertFieldValue(Object value) {
            return ((SimpleComboValue<Condition>)value).getValue();
        protected Object onConvertModelValue(Object value) {
            return combo.findModel((Condition)value);

    The rest (pretty ugly but it works)

    // Replace the text in the box with the enums label
    ListModelPropertyEditor<SimpleComboValue<Condition>> propEditor = 
        new ListModelPropertyEditor<SimpleComboValue<Condition>>()
             public String getStringValue(SimpleComboValue<Condition> value) {
                 return value.getValue().getLabel();
    // Replace the text in the list with the enums labels
    combo.getView().setModelProcessor(new ModelProcessor<SimpleComboValue<Condition>>() 
        public SimpleComboValue<Condition> prepareData(SimpleComboValue<Condition> model) 
            model.set("label", model.getValue().getLabel());
            return model;
    // Use label property in list as SimpleComboBox displayProperty "value" is used by default
    String html = "<tpl for=\".\"><div role=\"listitem\" class=\"" + combo.getListStyle() + "-item\">{" + "label" + "}</div></tpl>";

    If all used enums implemented a “Labelable” (or Internationalisable?) interface that required the getLabel() method then this code could be reused for different enums.

    I’m using GXT 2.2.3 and GWT 2.0.4.


    One response to “GXT Enums, ComboBoxes and i18n

    1. Pablo Vasquez May 27, 2011 at 3:52 am

      I implemented the labeling part (including i18n) overriding de toString() method in the enum. Found it to be pretty straightforward and not-that-ugly.

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out /  Change )

    Google photo

    You are commenting using your Google account. Log Out /  Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out /  Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out /  Change )

    Connecting to %s

    %d bloggers like this: