Anand Garlapati
Anand Garlapati

Reputation: 117

SimpleComboBox in GXT

How to use key/value pair using SimpleComboBox in GXT. I'm able to add a key to SimpleComboBox but how to add value of a particular key to a SimpleComboBox? Later I want to retrieve value of key. Thanks & Regards, Anand

Upvotes: 2

Views: 999

Answers (2)

Ahmed Adel
Ahmed Adel

Reputation: 66

The simple way to have key/value in a SimpleComboBox is to use a ListStore with type BaseModel. BaseModel allows you to save data as key/value like following:

SimpleComboBox combo = new SimpleComboBox();
ListStore<BaseModel> store = new ListStore<BaseModel>();

combo.setStore(store);
combo.setDisplayField("name");
// complete the SimpleComboBox properties here.

// Now, we will create data sample for the answer
BaseModel model = new BaseModel();
model.set("id", 1);
model.set("name", "Java");
store.add(model);

BaseModel model = new BaseModel();
model.set("id", 2);
model.set("name", "PHP");
store.add(model);

as you can see from the code snippet, BaseModel stores data as key/value pairs, so you can easily get the selected element from the SimpleComboBox like:

BaseModel selectedModel = (BaseModel) combo.getValue();
String techName = selectedModel.get("name"); // return value of key name

Upvotes: 1

ovie
ovie

Reputation: 621

// MySimpleComboBox extends SimpleComboBox
public MySimpleComboBox(){
    super(new ListStore<Person>(new ModelKeyProvider<Person>() {
        @Override
        public String getKey(Person item) {
            return item.id();
        }
    }), new LabelProvider<Person>() {
        @Override
        public String getLabel(Person item) {
            return item.toString();
        }
    });
}

I did that once I think it is what your looking for (or what you were looking for).

Upvotes: 0

Related Questions