staffan
17 Oct 2008, 12:34 AM
I'm using a ComboBox in GXT 1.1.
I've created a class, SelectionItemData, that represents an item in the ComboBox. I use ListStore assosiated with the ComboBox and adds a set of SelectionItemData objects to the ListStore using the add-method.
Later in my code I want to select an item in the ComboBox using the ComboBox.setValue() method. For some other reasons, not relevant here, I cannot access any of the instances that are already in the ComboBox. I've tried to solved this overriding the equals-method in my SelectionItemData so that I can select the disired value in the ComboBox. But the equals methos is never called when invoking setValue(), instead I get an extra item in my ComboBox. It seams like the ComboBox only can select an existing value if they are exactly the same objects. Why is the equals-method not used to determine if the new object is equal to an existing?
My code:
The SelectionItemData
public SelectionItemData(String code, String description) {
setDescription(description);
setCode(code);
}
public String getDescription() {
return (String) get("description");
}
public void setDescription(String description) {
set("description", description);
}
public String getCode() {
return (String) get("code");
}
public void setCode(String code) {
set("code", code);
}
@Override
public boolean equals(Object o) {
if(o instanceof SelectionItemData) {
SelectionItemData sid = (SelectionItemData)o;
if(sid.getCode() != null && this.getCode() != null && sid.getCode().equals(this.getCode())) returntrue;
}
return false;
}
Adding a set of SelectionItemData objects to the ComboBox:
tu = new ComboBox<SelectionItemData>();
tu.setDisplayField("description");
ListStore<SelectionItemData> baseStore = new ListStore<SelectionItemData>();
baseStore.add(controller.getSoi().getTUPartyPalletForDropDown());
Trying to a select an existing object in the ComboBox by making a new SelectionItemData with the same code. The overrided equals-method shall return true, but it is never called (checked with debug)
tu.setValue(new SelectionItemData("any existing code", "description");
I've created a class, SelectionItemData, that represents an item in the ComboBox. I use ListStore assosiated with the ComboBox and adds a set of SelectionItemData objects to the ListStore using the add-method.
Later in my code I want to select an item in the ComboBox using the ComboBox.setValue() method. For some other reasons, not relevant here, I cannot access any of the instances that are already in the ComboBox. I've tried to solved this overriding the equals-method in my SelectionItemData so that I can select the disired value in the ComboBox. But the equals methos is never called when invoking setValue(), instead I get an extra item in my ComboBox. It seams like the ComboBox only can select an existing value if they are exactly the same objects. Why is the equals-method not used to determine if the new object is equal to an existing?
My code:
The SelectionItemData
public SelectionItemData(String code, String description) {
setDescription(description);
setCode(code);
}
public String getDescription() {
return (String) get("description");
}
public void setDescription(String description) {
set("description", description);
}
public String getCode() {
return (String) get("code");
}
public void setCode(String code) {
set("code", code);
}
@Override
public boolean equals(Object o) {
if(o instanceof SelectionItemData) {
SelectionItemData sid = (SelectionItemData)o;
if(sid.getCode() != null && this.getCode() != null && sid.getCode().equals(this.getCode())) returntrue;
}
return false;
}
Adding a set of SelectionItemData objects to the ComboBox:
tu = new ComboBox<SelectionItemData>();
tu.setDisplayField("description");
ListStore<SelectionItemData> baseStore = new ListStore<SelectionItemData>();
baseStore.add(controller.getSoi().getTUPartyPalletForDropDown());
Trying to a select an existing object in the ComboBox by making a new SelectionItemData with the same code. The overrided equals-method shall return true, but it is never called (checked with debug)
tu.setValue(new SelectionItemData("any existing code", "description");