Selecting Items

Listing components displaying data also support allowing the user to select items. Depending on component, it the user can select either one or several items at a time.

How selection is handled in listings is split into three categories:

  • Single selection: components that only allow a single item to be selected at a time, for example RadioButtonGroup belongs in this category. More generally, all components that implement the SingleSelect interface belong in this category.

  • Multi selection: components that allow for selecting any number of the displayed items, for example CheckBoxGroup. All components that implement the MultiSelect interface belong in this category.

  • Listing components whose selection can be configured through the usage of the SelectionModel interface. The Grid component is an example of this type of listing and it currently has built in implementations for both the single selection and multi selection cases, as well as disabling selection altogether.

Single and Multi Selection

Single and multi selection components implement the HasValue interface, where the current selection represents the value that is currently held by the component. In practice this means that it is possible to get, set and listen to selection changes the same way you would with value changes in Vaadin field components. In the case of single select components HasValue is further extended with SingleSelect, and correspondingly with MultiSelect in the case of multi select components, giving further control over the current selection.

An example of basic single selection with the ComboBox component:

Java

  1. ComboBox<Availability> comboBox = new ComboBox<>();
  2. // Populate the combo box with items
  3. comboBox.setItems(EnumSet.allOf(Availability.class));
  4. // Set the current selection
  5. comboBox.setValue(Availability.DISCONTINUED);
  6. // Get the current selection
  7. Availability availability = comboBox.getValue();
  8. // Add a value change listener, a ValueChangeEvent<Availability> will be fired
  9. // any time a change to the selection is made.
  10. comboBox.addValueChangeListener(event -> Notification.show(event.getValue()));

A similar example for the multi select listing CheckBoxGroup follows. A difference to note in this example is the parameter type of setValue and the return type of getValue being Set<Category>, the members of which represent the selection contents.

Java

  1. CheckBoxGroup<Category> checkBoxGroup = new CheckBoxGroup<>();
  2. checkBoxGroup.setItems(EnumSet.allOf(Category.class));
  3. checkBoxGroup.setValue(EnumSet.allOf(Category.class));
  4. Set<Category> categories = checkBoxGroup.getValue();
  5. checkBoxGroup.addValueChangeListener(event -> {
  6. Notification.show("Number of selected items: " + event.getValue().size());
  7. });

Additionally, MultiSelect provides numerous utility functions for simpler programmatic handling of selections, such as:

Java

  1. checkBoxGroup.select(Category.DVD, Category.BOOK);
  2. checkBoxGroup.isSelected(Category.BOOK); // true
  3. checkBoxGroup.deselectAll();
  4. checkBoxGroup.getSelectedItems(); // now returns an empty set of Categories

Selection Models

Grid component can hold either multi- or single- selection. Since grid can not be both SingleSelect<SomePojo> and MultiSelect<SomePojo> in the same time, grid itself is not a select component, but it delegates the selection to a subclass of SelectionModel class. By default, Grid is in single selection mode, and we can obtain selection object using asSingleSelect method.

Java

  1. Grid<Person> grid = new Grid<>();
  2. SingleSelect<Person> selection = grid.asSingleSelect();
  3. //...
  4. Notification.show(selection.getValue().getName() + " was selected");

If selection of multiple rows is required, then Grid needs to be switched into multiselection mode, and multiple item selection object can be obtained using asMultiSelect method.

Java

  1. Grid<Person> grid = new Grid<>();
  2. grid.setSelectionMode(Grid.SelectionMode.MULTI);
  3. MultiSelect<Person> selection = grid.asMultiSelect();
  4. //...
  5. Notification.show(
  6. selection.getValue().stream().map(Person::getName).collect(Collectors.joining(", "))
  7. + " were selected");

Selected Items

Selection models (subclasses of SelectionModel) allow retrieving a HasValue object corresponding to the selection with the asSingleSelect and asMultiSelect methods, and thus can be used bound to data using a Binder. This way, conversions and validation can be used for selections.

Java

  1. public static class Company {
  2. private Person boss;
  3. private Set<Person> managers;
  4. public Person getBoss() {
  5. return boss;
  6. }
  7. public void setBoss(Person boss) {
  8. this.boss = boss;
  9. }
  10. public Set<Person> getManagers() {
  11. return managers;
  12. }
  13. public void setManagers(Set<Person> managers) {
  14. this.managers = managers;
  15. }
  16. }
  17. Binder<Company> companyBinder = new Binder<>();
  18. //Setup single selection binding
  19. Grid<Person> bossGrid = new Grid<>();
  20. SingleSelect<Person> bossSelection = bossGrid.asSingleSelect();
  21. companyBinder.forField(bossSelection).bind(Company::getBoss, Company::setBoss);
  22. //Setup multi selection binding
  23. Grid<Person> managersGrid = new Grid<>();
  24. managersGrid.setSelectionMode(Grid.SelectionMode.MULTI);
  25. MultiSelect<Person> managersSelection = managersGrid.asMultiSelect();
  26. companyBinder.forField(managersSelection).bind(Company::getManagers, Company::setManagers);

Selection Events

SelectionModel implementations allow retrieving a HasValue object corresponding to the selection with the asSingleSelect and asMultiSelect methods. The HasValue implementations returned by those methods support the standard addValueChangeListener method and all added listeners are notified about any selection change. In addition, selections support their own, selection-specific listeners, SelectionListener, SingleSelectionListener, and MultiSelectionListener. To add those listeners, we need to explicitly cast a selection to SingleSelectionModel, or MultiSelectionModel respectively.