BeanContainer + TwinColSelect - Populating RHS

Hi, I’m a newbie to Vaadin. I’m facing issues in setting default values for TwinColSelect. I have listed the code below. Please help me to fix this.

public class Portfolio {
  private long portfolioId;
  private String portfolioCode;
  private String portfolioName;
}

I have a master list of Portfolios on the LHS of TwinColSelect, which is working good.

BeanContainer<Long, Portfolio> portfoliosContainer = new BeanContainer<Long, Portfolio>(Portfolio.class);
portfoliosContainer.setBeanIdProperty("portfolioId");
portfoliosContainer.addAll(allPortfolios);

TwinColSelect portfolios = new TwinColSelect();
portfolios.setRequired(true);
portfolios.setLeftColumnCaption("Available Portfolios");
portfolios.setRightColumnCaption("Selected Portfolios");
portfolios.setContainerDataSource(portfoliosContainer);
portfolios.setItemCaptionMode(ItemCaptionMode.PROPERTY);
portfolios.setItemCaptionPropertyId("portfolioName");
portfolios.setWidth("700px");
portfolios.setImmediate(true);

On some action, I’m trying to set default values to RHS, by querying the portfoilos for a predefined group.

List<Portfolio> existingPortfolios = //query for list HashSet<Portfolio> preselected = new HashSet<Portfolio>(existingPortfolios); portfolios.setValue(preselected); I’m sure, that I have missed few basic steps in this. Appreciate your help in this.

Thanks,
Jagan

Hello Jagan,

The setValue() method of the TwinColSelect expects a set of Ids, which in this case are of type Long. To make your code work, you should then change the preselect-HashSet to contain the portfolioIds of the portfolios you wish to show on the RHS.

The code you’re using to set the selected value would actually work if you switched the container from BeanContainer to BeanItemContainer, which uses the beans themselves as ids. BeanItemContainer is simpler to use, so unless you have a specific need to specify the ids yourself, you should consider using it.

Thanks for the quick reply, Olli. I faced the same issue with BeanItemContainer eariler, so i tried to use BeanContainer.
The orginal snippet below.

[code]
BeanItemContainer portfoliosContainer = new BeanItemContainer(Portfolio.class, allPortfolios);

TwinColSelect portfolios = new TwinColSelect();
portfolios.setRequired(true);
portfolios.setLeftColumnCaption(“Available Portfolios”);
portfolios.setRightColumnCaption(“Selected Portfolios”);
portfolios.setContainerDataSource(portfoliosContainer);
portfolios.setItemCaptionMode(ItemCaptionMode.PROPERTY);
portfolios.setItemCaptionPropertyId(“portfolioName”);
portfolios.setWidth(“700px”);
portfolios.setImmediate(true);
[/code]I have overidden the equals and hashcode methods as well for the Portfolio class.

I take it it still doesn’t work? Do you get any expections when you call the setValue() method? Are you certain that existingPortfolios contains Portfolios that are in the container?

Yep… both the preselected set and the master list has the same objects.

masterPortfolio --> org.tiaacref.upg.model.Portfolio@814eef
masterPortfolio --> org.tiaacref.upg.model.Portfolio@19271a5
portfoliosToPreselect --> [org.tiaacref.upg.model.Portfolio@814eef, org.tiaacref.upg.model.Portfolio@19271a5]

After Overridng hasdcode and equals method

[code]
masterPortfolio → Portfolio [portfolioSeq=1397, portfolioCode=TEST1, portfolioName=TEST1]

masterPortfolio → Portfolio [portfolioSeq=201642, portfolioCode=TEST2, portfolioName=TEST2]

portfoliosToPreselect → [Portfolio [portfolioSeq=1397, portfolioCode=TEST1, portfolioName=TEST1]
, Portfolio [portfolioSeq=201642, portfolioCode=TEST2, portfolioName=TEST2]
]
[/code]I dont see any exceptions on calling setValue().

Unfortunately I am not able to reproduce the problem and I’m able to succesfully populate the RHS with your example code.

Could you provide the full code where you build the TwinColSelect and the part where you call setValue()?

Thanks for your time Olli. The issue was I had the TwinColSelect part of a Fieldgroup .

//portfolioGroupsfieldGroup.bind(portfoliosSelect, "portfolios"); After setting the TwinColSelect default values, I was again setting the values for portfolioGroupsfieldGroup.

BeanItem<PortfolioGroup> item = new BeanItem<PortfolioGroup>(group);
portfolioGroupsfieldGroup.setItemDataSource(item);

Once i removed the TwinColSelect from the fieldgroup, it worked.