Questions about getContainerProperty

Hi All,

I’m trying to understand the use of getContainerProperty.

I assumed that it was used to get an an item in the underlying data source. I am using a BeanItemContainer with a list of state objects. The state looks like:


public class StateMaster {
   private String statecode;
   private String statename;

   // setters and getters
}

I get these out of the database into a List and do an .addAll into the BeanItemContainer.

In the UI, I add the BeanItemContainer into a combobox.


ComboBox states = new ComboBox("State");
states.setContainerDataSource(addressDataService.getStateList());
states.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
states.setItemCaptionPropertyId("statename");

All of the above works fine. I get the stateNames displayed correctly, etc. Based on information gethered elsewhere, I have a state code. I want to show the state selected in the text portion of the combo box.

So, I am trying go get a hold of the state object in the data source for the combobox so that I can make it selected and the name of the state shows in the combo box.

I am trying:


StateMaster sm = states.getContainerProperty("CA","statecode");

The above ALWAYS returns null. I am guessing I don’t understand how to use that method.

So, what I am trying to do is:

  1. locate the object with a statecode = “CA” in the combo box data source
  2. show the selected statename (“California”) in the text field of the combo box (i.e. make it the selected state).

Any help and clarification would be appreciated.

Tom

The problem is that “CA” is not the itemId. The actual bean is used as the item id by BeanItemContainer. If you get a state code, e.g. “CA”, from somewhere else you need to loop through all items in the container until you find the itemId that has the “statecode” property set to “CA”. You can then set the ComboBox value to that itemId.

Or you could use a BeanContainer instead of a BeanItemContainer - it allows you to specify your own item identifiers or to use the value of a property as the item identifier.