Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

components-selection.asciidoc 11KB

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