PDA

View Full Version : New item created instead of selecting existing in ComboBox



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");

gslender
17 Oct 2008, 11:05 PM
why not just do...


ArrayList items = new ArrayList();
items.add(combo.getStore().getAt(index));
combo.setSelection(items);

you can always get the list of object from the combo... so just find the item you want selected and set it

cheers,
grant

staffan
20 Oct 2008, 1:05 AM
Still doesn't work (the way I want it). I rewrote my code so that I could found the actual instnce from the ListStore that I want to select. No difference, there are still an extra row in the comboBox as a copy of my selected item.

I've tried using both ComboBox.setValue() and ComboBox.setSelection(), no of them work. Look at the provided screenshot, I do not want multiple instances of the preselected item.

gslender
20 Oct 2008, 2:02 AM
staffan, post a complete example, that can be compiled without any extra classes. make sure the example explains/demonstrates your problem.

staffan
21 Oct 2008, 12:38 AM
I looked at all my other ComboBoxes and it seams like they all work in the same way: Regardless of what item that is currently selected, all the possible items are shown in the dropDown, including the selected item. Maybe this is the behavour that you expect in a ComboBox? Anyway, for allmost all my ComboBoxes (except the one in this example), this works ok. But the question is: is there a way to hide the current selection from the list of possible items?

A method something like "ComboBox.hideCurrentSelectionFromDropDown(boolean x)" would be nice..