![Vaadin 7 UI Design By Example:Beginner’s Guide](https://wfqqreader-1252317822.image.myqcloud.com/cover/861/36705861/b_36705861.jpg)
Time for action – fixing the OptionGroup example
It's time for our good deed of the day. OptionGroup
has a very simple method to turn on a multiple selection mode: setMultiSelect
.
- Open or import the optiongroup example in your IDE.
- Add
og.setMultiSelect(true)
after instantiatingog
. - Deploy and run the example.
What just happened?
Take a look at the new interface:
![](https://epubservercos.yuewen.com/A092AF/19470463008249006/epubprivate/OEBPS/Images/2261_02_21.jpg?sign=1738934317-ROsDqWidZS0IQ5gwH73BUa6htUpxArn3-0-4050bcb286ce64c6efc0ef6d6803fb9d)
As you can see, now the component is displayed as an assortment of checkboxes and the getValue
method (of both OptionGroup
and Property
) returns a Java Collection
(actually a Set
). This happens when we activate multiselect mode.
Note
We are not talking about Java EE, but just in case, the correct answers are:
Session beans and Message-driven beans
Have a go hero – improve the OptionGroup example
Try changing the OptionGroup
example application to show a message informing whether the answer is correct or not. Here is a quick hint: cast the value to a Java Set
instance.
Twin column selects
Instead of the OptionGroup
component we can use TwinColSelect
. We can change OptionGroup
for TwinColSelect
in the previous example:
TwinColSelect og = new TwinColSelect( "Two kinds of EJBs in Java EE are:", answers);
With this, we get an interesting component for selection:
![](https://epubservercos.yuewen.com/A092AF/19470463008249006/epubprivate/OEBPS/Images/2261_02_22.jpg?sign=1738934317-dPQLB6yaZR5WnFzf3VBnNMOvPqFm5pf3-0-bc29842f8a1b0995a458c3a7185a5d60)
.
Date/time pickers
Vaadin includes a nice date/time picker with multiple configuration options. You can add a date picker using this:
DateField dateField = new DateField("Select a date");
This will display a field to directly type the date and a button to open a convenient calendar:
![](https://epubservercos.yuewen.com/A092AF/19470463008249006/epubprivate/OEBPS/Images/2261_02_23.jpg?sign=1738934317-Ke0eHR5rfHAeySQNSPeq5JPJWHR450gg-0-68e1611b96c7da2aa03d64c934f6bdb7)
The getValue
method of DateField
returns a java.util.Date
object. You can configure the resolution of DateField
. For example, to allow users select only a year, we can do this:
dateField.setResolution(Resolution.YEAR);
This will show a more restrictive component:
![](https://epubservercos.yuewen.com/A092AF/19470463008249006/epubprivate/OEBPS/Images/2261_02_24.jpg?sign=1738934317-FI6b0LGCovI8TJUGq1EXRt3KSOskrkBr-0-6533ef6d5ba3a0cba7b2af34ca37b591)
Note
Resolution
is a Java enum that includes the YEAR
, MONTH
, DAY
, HOUR
, MINUTE
, and SECOND
values to use in your date pickers.
A date format can be specified using the setDateFormat
method:
dateField.setDateFormat("yyyy-MM-dd");
Note
yyyy-MM-dd
is the date format for the ISO 8601 standard.
Note
If you want to display the entire calendar selector (instead of the input field with the button to open a calendar) use InlineDateField
.