Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
TwinColSelect Pre-Populate RHS
Hi,
I'm wondering, is it possible to pre-populate the RHS of the TwinColSelect with some values....or....perhaps I could "mimic" the select event before displaying the component so that the RHS has some values?
Basically I'm assigning some "roles" to a user, but naturally if that user already has those roles, then it doesn't make sense for these roles to be displayed on the LHS, then should be on the RHS.
-=bootlaces=-
What you are looking for is
setValue()
which takes as input a collection of the selected item ids.
Note that the parameter for the setValue() must be a Set for multiselect components. Like this:
final TwinColSelect select = new TwinColSelect("Select Targets to Destroy");
// Put some data in the select
String planets[] = {"Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};
for (int pl=0; pl<planets.length; pl++)
select.addItem(planets[pl]);
// Preselect a few items
HashSet<String> preselected = new HashSet<String>();
preselected.add("Venus");
preselected.add("Earth");
preselected.add("Mars");
select.setValue(preselected);
Marko Grönroos: Note that the parameter for the setValue() must be a Set for multiselect components. Like this:
Internal setValue method taken from AbstractSelect:
protected void setValue(Object newValue, boolean repaintIsNotNeeded)
throws Property.ReadOnlyException, Property.ConversionException {
if (isMultiSelect()) {
if (newValue == null) {
super.setValue(new HashSet(), repaintIsNotNeeded);
} else if (Collection.class.isAssignableFrom(newValue.getClass())) {
super.setValue(new HashSet((Collection) newValue),
repaintIsNotNeeded);
}
} else if (newValue == null || items.containsId(newValue)) {
super.setValue(newValue, repaintIsNotNeeded);
}
}
This would indicate that the actual value given as the parameter to setValue doesn't have to be a Set, any collection should suffice, the collection will be converted to a Set. The getValue will return a Set of selected item ids.
I too thought at a Collection would suffice, but when I tried with Vector, I couldn't get it working. A bit odd, maybe I did something wrong.