PDA

View Full Version : How to set default value to ComboBox



treblereel
24 Jan 2012, 11:56 PM
Hi folks,

I have problems while i am trying to set default value to ComboBox using departmentComboBox.setValue(departmentStore.getAt(0)). I think, it doesnt work coz store is empty at the moment. How can i fix it ?



private ComboBox<BeanModel> doDepartmentComboBox(){
ListStore<BeanModel> departmentStore;
final DepartmentServiceAsync departmentService = GWT.create(DepartmentService.class);
ListLoader<?> loader;

RpcProxy<List<DepartmentDTO>> DepartmentProxy = new RpcProxy<List<DepartmentDTO>>() {
@Override
protected void load(Object loadConfig, AsyncCallback<List<DepartmentDTO>> callback) {
departmentService.getDepartments("1",callback);
}
};

loader = new BaseListLoader<ListLoadResult<ModelData>>(DepartmentProxy,new BeanModelReader());
departmentStore = new ListStore<BeanModel>(loader);
departmentStore.setMonitorChanges(true);
loader.load();


departmentComboBox = new ComboBox<BeanModel>();
departmentComboBox.setDisplayField("name");
departmentComboBox.setWidth(150);
departmentComboBox.setName("department");
departmentComboBox.setFieldLabel("Department");
departmentComboBox.setAllowBlank(false);
departmentComboBox.setEditable(false);
departmentComboBox.setValue(departmentStore.getAt(0));
departmentComboBox.setStore(departmentStore);
departmentComboBox.setTypeAhead(true);
departmentComboBox.setTriggerAction(TriggerAction.ALL);

return departmentComboBox;
}

harun
25 Jan 2012, 6:14 AM
You must set value after loading data. so you can add setValue(...) method after callback response.

I advice you:




RpcProxy<List<DepartmentDTO>> DepartmentProxy = new RpcProxy<List<DepartmentDTO>>() {
@Override
protected void load(Object loadConfig, AsyncCallback<List<DepartmentDTO>> callback) {
departmentService.getDepartments("1",callback);


departmentComboBox.setValue(departmentStore.getAt(0));


}
};


Best Regards,

Colin Alworth
25 Jan 2012, 3:22 PM
Right idea harun, but I would move that to a listener on the Loader.Load event coming from the loader instance, so that you know that the server has already sent back the results, instead of setting the value as soon as the request is made.

treblereel
27 Jan 2012, 5:51 AM
thanks for answer. but combox is populating only after i click to combo. How do i can load and set value after attach ? something like Events.attach => combo.getStore.getLoader.load ?

harun
27 Jan 2012, 5:54 AM
Hi, you can easily listen attach event like:




myComboBox.addListener( Events.Attach, new Listener<BaseEvent>() {
@Override
public void handleEvent( BaseEvent be)
{
// your code what you want
}
} );