Application of multi-select items?

I have a UI that displays a multi-select list representing a single object with a list of selected items. This object looks something like this:

class Attribute {
private String validValues;
private String selectedValues;

public String[] getValidValues() {
    return validValues;
}
public void setValidValues(String[] values) {
    validValues = values;
}

public String[] getSelectedValues() {
    return selectedValues;
}
public void setSelectedValues(String[] values) {
    selectedValues = values;
}

}

How would this be mapped to a Select UI item to ensure the user selections are saved back to the object correctly?

Thanks!
Scott

Hi,

You could set the object state in a ValueChangeListener, eg.

select.addListener(new ValueChangeListener() {
	@Override
	public void valueChange(ValueChangeEvent event) {
		Object values = select.getValue();

		if (values instanceof Set) {
			// Multiple selected values returned as a Set
			List<String> list = new ArrayList<String>((Set) values);
			attribute.setSelectedValues(list.toArray(new String[list
					.size()]));
		} else if (values != null) {
			// If only one value is selected it will be a String
			attribute.setSelectedValues(new String[] { values
					.toString() });
		} else {
			attribute.setSelectedValues(null);
		}
	}
});

Hope this helps,
Henri

Thanks, Henri!

Is this the standard and/or best manner of capturing the selected values? The reason I ask is I have been using BeanItem for most properties and it seems to work quite well. For a Select, I had expected to be able to use assign it to some manner of method that takes a String (or maybe Object
) of selected items. This seems like it would be a common use case.

Thanks again!
Scott

Our R&D guys will correct me if I’m wrong, but as far as I know there is no standard for autowiring such an attribute set to a Select. You are right that it is a common need, and a simple pattern implementation in the library could cover >90% of the cases. If you write a feature request ticket about it, maybe it’ll appear in 6.3 :slight_smile: