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.
getValue in Table or Tree
Hi,
I don't completely understand how the getValue works in components like Table or Tree.
If they are in single select mode, then I get whatever Object corresponds to that row or node, but if it is multiselect? Do I get a list of Objects? And if it's multiselect but there is only a value selected, do I get a list with one element or I get the value Object?
How do I know if there is more than one selected row or node? Something like this would be a really bad solution for many reasons:
if (table.isMultiSelect())
Collection<Object> values = (Set<Object>) table.getValue();
else
Object value = table.getValue();
Can somebody tell me if my understandings are correct?
Hi,
If you look at the getValue method of the i.e Table :
Incase of single select you will get only one Item of the Type you set the value with.
Incase of multiselect you wil get a List of objects. no matter if none,one or many items are selected.
so your can override the method and always return a collection incase your application requires that.
final Object retValue = super.getValue();
if (isMultiSelect()) {
// If the return value is not a set
if (retValue == null) {
return new HashSet<Object>();
}
if (retValue instanceof Set) {
return Collections.unmodifiableSet((Set<?>) retValue);
} else if (retValue instanceof Collection) {
return new HashSet<Object>((Collection<?>) retValue);
} else {
final Set<Object> s = new HashSet<Object>();
if (items.containsId(retValue)) {
s.add(retValue);
}
return s;
}
} else {
return retValue;
}
I thought that it was a little bit confusing, but I see how to handle it now, thank you!