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.5KB

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