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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. ---
  2. title: Selection Components
  3. order: 5
  4. layout: page
  5. ---
  6. [[components.selection]]
  7. = Selection Components
  8. Vaadin offers many alternative ways for selecting one or more items. The core
  9. library includes the following selection components, all based on the
  10. [classname]#AbstractSelect# class:
  11. // TODO Only use section numbers here, prefixed with "Section", not include section title
  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 an input prompt 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]#Tree#, [classname]#Table#, and [classname]#TreeTable# components allow special forms of selection.
  27. They also inherit [classname]#AbstractSelect#.
  28. [[components.selection.databinding]]
  29. == Binding Selection Components to Data
  30. The selection components typically bound to list of items obtained from backend system.
  31. You can give the the list of items in the constructor or set it set
  32. [methodname]#setItems()#. Read more in
  33. <<dummy/../../../framework/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]#getSelected# defined in
  37. [interfacename]#Select# interface. Also selection changes are handled as
  38. selection change events, as is described later.
  39. [[components.selection.adding]]
  40. == Adding New Items
  41. New items are added with the [methodname]#addItems()# method.
  42. [source, java]
  43. ----
  44. // Create a selection component
  45. ComboBox<String> select = new ComboBox<>("Select a planet");
  46. // Add items to select
  47. select.setItems("Mercury","Venus","Earth");
  48. ----
  49. [[components.selection.captions]]
  50. == Item Captions
  51. The items are typically a strings, in which case they can be used as the
  52. caption, but can be any object type. We could as well have given Planet instances
  53. for the items and use captions generated based on them
  54. [methodname]#setItemCaptionProvider()# method.
  55. [source, java]
  56. ----
  57. // List of Planet objects
  58. List<Planet> planets = new ArrayList<>();
  59. planets.add(new Planet("Mercury"));
  60. planets.add(new Planet("Venus"));
  61. planets.add(new Planet("Earth"));
  62. // Create a selection component
  63. ComboBox<Planet> select = new ComboBox<>("My Select");
  64. // Add an items to the ComboBox
  65. select.setItems(planets);
  66. select.setItemCaptionProvider(planet -> planet.getName());
  67. // or even select.setItemCaptionProvider(Planet::getName);
  68. // Select the first
  69. select.select(planets.get(0));
  70. ----
  71. In addition to a caption, an item can have an icon. The icon is set with
  72. [methodname]#setItemIconProvider()#.
  73. Typical use cases for captions are:
  74. Using the items as the caption: the caption is
  75. retrieved with [methodname]#toString()# method from the item. This is useful
  76. for simple objects like String or Integers, but also for objects that have
  77. human readable output for [methodname]#toString()# .
  78. [source, java]
  79. ----
  80. ComboBox<Planet> select = new ComboBox<>("Inner Planets");
  81. // A class that implements toString()
  82. class Planet implements Serializable {
  83. String planetName;
  84. Planet(String name) {
  85. planetName = name;
  86. }
  87. public String toString () {
  88. return "The Planet " + planetName;
  89. }
  90. }
  91. // Use such objects as items
  92. List<Planet> planets = new ArrayList<>();
  93. planets.add(new Planet("Mercury"));
  94. planets.add(new Planet("Venus"));
  95. planets.add(new Planet("Earth"));
  96. select.addItems(planets);
  97. ----
  98. Using a field of a item as caption: the caption is retrieved using the
  99. [interfacename]#ItemCaptionProvider# typically given as Java 8 lambda:
  100. INDEX::
  101. Index number of item is used as caption.
  102. This caption mode is applicable only to data sources that implement the [interfacename]#Container.Indexed# interface.
  103. If the interface is not available, the component will throw a
  104. [classname]#ClassCastException#.
  105. The [classname]#AbstractSelect# itself does not implement this interface, so the mode is not usable without a separate data source.
  106. An [classname]#IndexedContainer#, for example, would work.
  107. ITEM:: [classname]#String# representation of item, acquired with
  108. [methodname]#toString()#, is used as the caption. This is applicable mainly when
  109. using a custom [classname]#Item# class, which also requires using a custom
  110. [classname]#Container# that is used as a data source for the selection
  111. component.
  112. PROPERTY:: Item captions are read from the [classname]#String# representation of the
  113. property with the identifier specified with
  114. [methodname]#setItemCaptionPropertyId()#. This is useful, for example, when you
  115. have a container that you use as the data source for the selection component,
  116. and you want to use a specific property for caption.
  117. +
  118. In the example below, we bind a selection component to a bean container and use
  119. a property of the bean as the caption.
  120. +
  121. [source, java]
  122. ----
  123. // A class that implements toString()
  124. class Planet implements Serializable {
  125. Integer id;
  126. String planetName;
  127. Planet(Integer id, String name) {
  128. this.id = id
  129. this.planetName = name;
  130. }
  131. public String toString () {
  132. return "The Planet " + planetName;
  133. }
  134. public Integer getId () {
  135. return id;
  136. }
  137. public String getName () {
  138. return planetName;
  139. }
  140. }
  141. // Put some example data
  142. List<Planet> planets = new ArrayList<>();
  143. planets.add(new Planet(1, "Mercury"));
  144. planets.add(new Planet(2, "Venus"));
  145. planets.add(new Planet(3, "Earth"));
  146. planets.add(new Planet(4, "Mars"));
  147. // Create a selection component
  148. ComboBox<Planet> select =
  149. new ComboBox<>("Planets");
  150. // Set the caption provider to read the
  151. // caption directly from the 'name'
  152. // property of the bean
  153. select.setItemCaptionProvider(Planet::getName);
  154. ----
  155. [[components.selection.getset]]
  156. == Getting and Setting Selection
  157. You can get the item with [methodname]#getSelected()# of the
  158. [classname]#Select# interface that returns collection of selected items.
  159. You can select an item with the corresponding [methodname]#select()# method.
  160. In multiselect mode, the [methodname]#getSelected()# returns an unmodifiable
  161. set of items. If no item is selected, the select will be an empty collection.
  162. The [classname]#ComboBox# and [classname]#NativeSelect# will show empty
  163. selection when no actual item is selected.
  164. [[components.selection.valuechange]]
  165. == Handling Selection Changes
  166. You can access currently selected item with the [methodname]#getSelected()# or
  167. [methodname]#getFirstSelected()# method of the component. Also, when
  168. handling selection changes with a
  169. [classname]#SelectionChangeListener#, the
  170. [classname]#SelectionChange# will have the selected items of the event.
  171. [source, java]
  172. ----
  173. // Create a selection component with some items
  174. ComboBox<String> select = new ComboBox<>("My Select");
  175. select.setItems("Io", "Europa", "Ganymedes", "Callisto");
  176. // Handle selection change
  177. select.addSelectionChangeListener(event ->
  178. layout.addComponent(new Label("Selected " +
  179. event.getSelected())));
  180. ----
  181. The result of user interaction is shown in
  182. <<figure.components.selection.valuechange>>.
  183. [[figure.components.selection.valuechange]]
  184. .Selected Item
  185. image::img/select-selected1.png[width=30%, scaledwidth=40%]
  186. [[components.selection.newitems]]
  187. == Allowing Adding New Items
  188. [classname]#ComboBox# allows the user to add new items, when the user types
  189. in a value and presses kbd:[Enter]. You need to enable this with
  190. [methodname]#setNewItemHandler()#.
  191. Adding new items is not possible if the selection component is read-only. An
  192. attempt to do so may result in an exception.
  193. [[components.selection.newitems.handling]]
  194. === Handling New Items
  195. Adding new items is handled by a [interfacename]#NewItemHandler#, which gets the
  196. item caption string as parameter for the [methodname]#addNewItem()# method.
  197. ifdef::web[]
  198. [source, java]
  199. ----
  200. // List of planets
  201. List<Planet> planets = new ArrayList<>();
  202. planets.add(new Planet(1, "Mercury"));
  203. planets.add(new Planet(2, "Venus"));
  204. planets.add(new Planet(3, "Earth"));
  205. planets.add(new Planet(4, "Mars"));
  206. ComboBox<Planet> select =
  207. new ComboBox<>("Select or Add a Planet");
  208. select.setItems(planets);
  209. // Use the name property for item captions
  210. select.setItemCaptionProvider(Planet::getName);
  211. // Allow adding new items and add
  212. // handling for new items
  213. select.setNewItemHandler(inputString -> {
  214. // Create a new bean - can't set all properties
  215. Planet newPlanet = new Planet(0, inputString);
  216. planets.add(newPlanet);
  217. // Update combobox content
  218. select.setItems(planets);
  219. // Remember to set the selection to the new item
  220. select.select(newPlanet);
  221. Notification.show("Added new planet called " +
  222. inputString);
  223. });
  224. ----
  225. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.select.combobox.newitemhandler[on-line example, window="_blank"].
  226. endif::web[]
  227. [[components.selection.multiple]]
  228. == Multiple Selection
  229. Some selection components, such as [classname]#CheckBoxGroup#,
  230. [classname]#ListSelect# and [classname]#TwinColSelect# are multiselect components,
  231. they extend [classname]#AbstractMultiSelect# class.
  232. Multiselect components use the [interfacename]#SelectionModel.Multi# selection model.
  233. This model allows to select multiple items.
  234. You can get and set the selection with the [methodname]#SelectionModel.getSelectedItems()# and
  235. [methodname]#SelectionModel.Multi.selectItems()# methods.
  236. A change in the selection will trigger a [classname]#SelectionChange#, which
  237. you can handle with a [classname]#SelectionChangeListener#. The
  238. following example shows how to handle selection changes with a listener.
  239. [source, java]
  240. ----
  241. // A selection component with some items
  242. ListSelect<String> select = new ListSelect<>("My Selection");
  243. select.setItems("Mercury", "Venus", "Earth",
  244. "Mars", "Jupiter", "Saturn", "Uranus", "Neptune");
  245. // Feedback on value changes
  246. select.addSelectionListener(event -> {
  247. // Some feedback
  248. layout.addComponent(new Label("Selected: " +
  249. event.getNewSelection()));
  250. }
  251. });
  252. ----
  253. [[components.selection.item-icons]]
  254. == Item Icons
  255. You can set an icon for each item with [methodname]#setItemIconProvider()#,
  256. in a fashion similar to captions. Notice, however, that icons are not
  257. supported in [classname]#NativeSelect#, [classname]#TwinColSelect#, and
  258. some other selection components and modes. This is because HTML does not
  259. support images inside the native [literal]#++select++#
  260. elements. Icons are also not really visually applicable.