You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

components-selection.asciidoc 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. ---
  2. title: Selection Components
  3. order: 5
  4. layout: page
  5. ---
  6. [[components.selection]]
  7. = Selection Components
  8. For a better overview on how selection works, see link:../datamodel/datamodel-selection.asciidoc[Selecting items]
  9. Vaadin offers many alternative ways for selecting one or more items. The core
  10. library includes the following selection components, all based on either
  11. [classname]#AbstractSingleSelect# or [classname]#AbstractMultiSelect# class:
  12. [classname]#ComboBox# (<<components-combobox#components.combobox,"ComboBox">>)::
  13. A drop-down list with a text box, where the user can type text to find matching items.
  14. The component also provides a placeholder and the user can enter new items.
  15. [classname]#ListSelect# (<<components-listselect#components.listselect,"ListSelect">>)::
  16. A vertical list box for selecting items in either single or multiple selection mode.
  17. [classname]#NativeSelect# (<<components-nativeselect#components.nativeselect, "NativeSelect">>)::
  18. Provides selection using the native selection component of the browser, typically a drop-down list for single selection and a multi-line list in multiselect mode.
  19. This uses the [literal]#++<select>++# element in HTML.
  20. [classname]#RadioButtonGroup# (<<components-optiongroups#components.optiongroups,"CheckBoxGroup and RadioButtonGroup">>)::
  21. Shows the items as a vertically arranged group of radio buttons in single selection mode.
  22. [classname]#CheckBoxGroup# (<<components-optiongroups#components.optiongroups,"CheckBoxGroup and RadioButtonGroup">>)::
  23. Shows the items as a vertically arranged group of check boxes in multiple selection mode.
  24. [classname]#TwinColSelect# (<<components-twincolselect#components.twincolselect, "TwinColSelect">>)::
  25. Shows two list boxes side by side where the user can select items from a list of available items and move them to a list of selected items using control buttons.
  26. In addition, the [classname]#Grid# component allows user selection.
  27. [[components.selection.databinding]]
  28. == Binding Selection Components to Data
  29. The selection components are typically bound to list of items obtained from backend system.
  30. You can give the list of items in the constructor or set it with
  31. [methodname]#setItems()#. Read more in
  32. <<dummy/../../../framework/datamodel/datamodel-overview.asciidoc#datamodel.overview,"Binding
  33. Components to Data">>.
  34. You can get the current selection as the
  35. value of the selection component using [methodname]#getValue# defined in
  36. [interfacename]#HasValue# interface. Selection changes are handled as value change or
  37. selection events, as is described later.
  38. [[components.selection.captions]]
  39. == Item Captions
  40. The items are typically a strings, in which case they can be used as the
  41. caption, but can be any object type. We could as well have given Planet instances
  42. for the items and use captions generated based on them
  43. [methodname]#setItemCaptionGenerator()# method.
  44. [source, java]
  45. ----
  46. // List of Planet objects
  47. List<Planet> planets = new ArrayList<>();
  48. planets.add(new Planet("Mercury"));
  49. planets.add(new Planet("Venus"));
  50. planets.add(new Planet("Earth"));
  51. // Create a selection component
  52. ComboBox<Planet> select = new ComboBox<>("My Select");
  53. // Add an items to the ComboBox
  54. select.setItems(planets);
  55. select.setItemCaptionGenerator(Planet::getName);
  56. // Select the first
  57. select.setSelectedItem(planets.get(0));
  58. // or
  59. // select.setValue(planets.get(0));
  60. ----
  61. In addition to a caption, an item can have an icon. The icon is set with
  62. [methodname]#setItemIconGenerator()#.
  63. Typical use cases for captions are:
  64. Using the items as the caption: the caption is
  65. retrieved with [methodname]#toString()# method from the item. This is useful
  66. for simple objects like String or Integers, but also for objects that have
  67. human readable output for [methodname]#toString()# .
  68. Using a field of a item as caption: the caption is retrieved using the
  69. [interfacename]#ItemCaptionGenerator# typically given as a lambda or a method reference.
  70. [[components.selection.item-icons]]
  71. == Item Icons
  72. You can set an icon for each item with [methodname]#setItemIconGenerator()#,
  73. in a fashion similar to captions. Notice, however, that icons are not
  74. supported in [classname]#NativeSelect#, [classname]#TwinColSelect#, and
  75. some other selection components and modes. This is because HTML does not
  76. support images inside the native [literal]#++select++#
  77. elements.
  78. [[components.selection.getset]]
  79. == Getting and Setting Selection
  80. For a better overview on how selection works, see link:../datamodel/datamodel-selection.asciidoc[Selecting items]
  81. You can get selected the item with [methodname]#getValue()# of the
  82. [interfacename]#HasValue# interface that returns either a single selected item
  83. (case of [interfacename]#SingleSelect#) or a collection of selected items (case of [interfacename]#MultiSelect#).
  84. You can select an item with the corresponding [methodname]#setValue()# method.
  85. The [classname]#ComboBox# and [classname]#NativeSelect# will show empty
  86. selection when no actual item is selected.
  87. [[components.selection.valuechange]]
  88. == Handling Selection Events
  89. You can access the currently selected item with the [methodname]#getValue()# ([interfacename]#SingleSelect#) or
  90. [methodname]#getSelectedItems()# ([interfacename]#MultiSelect#) method of the component. Also, when
  91. handling selection events with a
  92. [classname]#SelectionListener#, the
  93. [classname]#SelectionEvent# will have the selected items of the event. Single- and Multiselect
  94. components have their own more specific listener and event types, [interfacename]#SingleSelectionListener# for [classname]#SingleSelectionEvent# and [interfacename]#MultiSelectionListener# for [classname]#MultiSelectionEvent# respectively. Both can be added with the [methodname]#addSelectionListener# method.
  95. [source, java]
  96. ----
  97. // Create a selection component with some items
  98. ComboBox<String> select = new ComboBox<>("My Select");
  99. select.setItems("Io", "Europa", "Ganymedes", "Callisto");
  100. // Handle selection event
  101. select.addSelectionListener(event ->
  102. layout.addComponent(new Label("Selected " +
  103. event.getSelectedItem().orElse("none")));
  104. ----
  105. The result of user interaction is shown in
  106. <<figure.components.selection.valuechange>>.
  107. [[figure.components.selection.valuechange]]
  108. .Selected Item
  109. image::img/select-selected1.png[width=30%, scaledwidth=40%]
  110. [[components.selection.multiple]]
  111. == Multiple Selection
  112. For a better overview on how selection works, see link:../datamodel/datamodel-selection.asciidoc[Selecting items]
  113. Some selection components, such as [classname]#CheckBoxGroup#,
  114. [classname]#ListSelect# and [classname]#TwinColSelect# are multiselect components,
  115. they extend [classname]#AbstractMultiSelect# class.
  116. Multiselect components use the [interfacename]#MultiSelect# interface which extends [interfacename]#HasValue#.
  117. This provides more fine grained API for selection. You can get and set the selection with the [methodname]#getSelectedItems()# and
  118. [methodname]#select()# methods.
  119. A change in the selection will trigger a [classname]#SelectionEvent#, which
  120. you can handle with a [classname]#SelectionListener#. The
  121. following example shows how to handle selection changes with a listener.
  122. [source, java]
  123. ----
  124. // A selection component with some items
  125. ListSelect<String> select = new ListSelect<>("My Selection");
  126. select.setItems("Mercury", "Venus", "Earth",
  127. "Mars", "Jupiter", "Saturn", "Uranus", "Neptune");
  128. // Feedback on value changes
  129. select.addSelectionListener(event -> {
  130. // Some feedback
  131. layout.addComponent(new Label("Selected: " +
  132. event.getNewSelection()));
  133. }
  134. });
  135. ----