baluba
24 Apr 2008, 5:05 AM
The example code below illustrates some problems with the ComboBox:
When the combox is created its Store must have at least one item, otherwise the Combo wont display anything, even if the store is changed in a listener etc.
.
Calling removeAll on a ComboBox's Store doesn't remove the item that was part of the Store when combobox was created.
.
In the example attached, if you click the ComboBox before you click the button, the ComboBox will not display properly after you add items by clicking the button.
.
If you click the button first, the combo will layout ok, but the "dummy" item is still there.
public class Main implements EntryPoint {
private Viewport viewport;
public class MyModel extends BaseModel {
public MyModel() {
}
public MyModel(String name, Integer age) {
set("name", name);
set("age", age);
}
}
public void onModuleLoad() {
viewport = new Viewport();
viewport.setLayout(new FlowLayout());
Store store = new Store();
// if next line is commented out the combo doesn't display any items
store.add(new Record(new MyModel("dummy", 5)));
final ComboBox combo = new ComboBox();
combo.setWidth(100);
combo.fieldLabel = "whatever";
combo.displayField = "name";
// if next line is commented out the combo doesn't display any items
combo.setStore(store);
Button btn = new Button("Fill combo", new SelectionListener() {
public void componentSelected(ComponentEvent ce) {
Store s = combo.getStore();
// following command doesn't work, "dummy" item still visible in list
s.removeAll();
s.add(new Record(new MyModel("John1", 30)));
s.add(new Record(new MyModel("John2", 31)));
s.add(new Record(new MyModel("John3", 32)));
s.add(new Record(new MyModel("John4", 33)));
combo.recalculate(); // no effect
}
});
viewport.add(combo);
viewport.add(btn);
viewport.layout(true);
RootPanel.get().add(viewport);
}
}
When the combox is created its Store must have at least one item, otherwise the Combo wont display anything, even if the store is changed in a listener etc.
.
Calling removeAll on a ComboBox's Store doesn't remove the item that was part of the Store when combobox was created.
.
In the example attached, if you click the ComboBox before you click the button, the ComboBox will not display properly after you add items by clicking the button.
.
If you click the button first, the combo will layout ok, but the "dummy" item is still there.
public class Main implements EntryPoint {
private Viewport viewport;
public class MyModel extends BaseModel {
public MyModel() {
}
public MyModel(String name, Integer age) {
set("name", name);
set("age", age);
}
}
public void onModuleLoad() {
viewport = new Viewport();
viewport.setLayout(new FlowLayout());
Store store = new Store();
// if next line is commented out the combo doesn't display any items
store.add(new Record(new MyModel("dummy", 5)));
final ComboBox combo = new ComboBox();
combo.setWidth(100);
combo.fieldLabel = "whatever";
combo.displayField = "name";
// if next line is commented out the combo doesn't display any items
combo.setStore(store);
Button btn = new Button("Fill combo", new SelectionListener() {
public void componentSelected(ComponentEvent ce) {
Store s = combo.getStore();
// following command doesn't work, "dummy" item still visible in list
s.removeAll();
s.add(new Record(new MyModel("John1", 30)));
s.add(new Record(new MyModel("John2", 31)));
s.add(new Record(new MyModel("John3", 32)));
s.add(new Record(new MyModel("John4", 33)));
combo.recalculate(); // no effect
}
});
viewport.add(combo);
viewport.add(btn);
viewport.layout(true);
RootPanel.get().add(viewport);
}
}