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.
How to hook up OptionGroup to a BeanItem?
I am so confused about how to use OptionGroup (radio buttons).
I know how to hook up a String values in a domain object to a BeanItem, and from there set the properties of the BeanItem as the data source for various TextField objects on a layout.
[DomainObject member variables] <--> [JavaBean getter-setter conventions or BeanInfo] <--> [BeanItem properties] <--> [TextFields data sources]
But I can't seem to wrap my head around how to do the same with the Select widgets such as OptionGroup.
What I want is 3 radio buttons that map to an integer value:
( ) Yes
( ) No
( o ) Not yet answered
Those radio buttons should map to an Integer property on my BeanItem, where:
Yes = 1
No = 0
Not yet answered = -1
Can someone give me a clue about the easy way to do this? I've found some hard ways. :(
--Basil Bourque
I think I know how to properly construct the OptionGroup:
// Create the possible values
Collection<Integer> trivalentValues = new ArrayList<Integer>(3);
trivalentValues.add( new Integer(1) );
trivalentValues.add(new Integer(0) );
trivalentValues.add(new Integer(-1));
// Create the widget
this.aliveRadios = new OptionGroup("Are you alive:", trivalentValues );
this.aliveRadios.setItemCaption(1,"Yes");
this.aliveRadios.setItemCaption(0,"No");
this.aliveRadios.setItemCaption(-1,"Not yet answered");
But I don't understand how to hook this up to the Integer member of a BeanItem.
--Basil Bourque
Hi Basil,
you need two thing to bind an Optiongroup.
First, you need to set a container ( or add manually like you did) to the OptionGroup.
Second, you do the normal binding like any Vaadin component.
from your example :
// Create the possible values
Collection<Integer> trivalentValues = new ArrayList<Integer>(3);
trivalentValues.add( new Integer(1) );
trivalentValues.add(new Integer(0) );
trivalentValues.add(new Integer(-1));
// Create the widget
this.aliveRadios = new OptionGroup("Are you alive:", trivalentValues );
this.aliveRadios.setItemCaption(1,"Yes");
this.aliveRadios.setItemCaption(0,"No");
this.aliveRadios.setItemCaption(-1,"Not yet answered");
this.aliveRadios.setPropertyDataSource(beanItem.getItemProperty("yourIntegerProperty");
Don't forget to set the immediate mode
setImmediate(true)
.
Did you consider binding the optionGroup with an enumeration instead of an Integer ?
Regards.
Éric
Thanks to Éric for the suggestion to consider using an enum. (I assume he meant an enum rather than Enumeration.) I did indeed learn how to use an enum to back an OptionGroup (radio buttons). Works rather well.
See my answer on StackOverflow for explanation and a couple of complete example apps showing how to populate an OptionGroup with an enum.