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.

ConfigureComboBoxesWisely.asciidoc 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ---
  2. title: Configure Combo Boxes Wisely
  3. order: 5
  4. layout: page
  5. ---
  6. [[configure-comboboxes-wisely]]
  7. Configure ComboBoxes wisely
  8. ---------------------------
  9. The Vaadin *ComboBox* is a versatile input field with lots of settings
  10. that change its behavior. The default settings are a good start, but are
  11. not necessarily suitable for all situations. Configure your ComboBoxes
  12. properly to avoid usability issues and make use of their advanced
  13. features.
  14. image:img/combo2.png[ComboBox]
  15. [[null-selection]]
  16. Null selection
  17. ^^^^^^^^^^^^^^
  18. By default, the *ComboBox* component has null selection enabled,
  19. which means that the drop down list contains an *empty element* that
  20. maps to `null`. If you don’t want users to be able to select _“none”_,
  21. you should disable null selection with `setNullSelectionAllowed(false)`.
  22. If you _do_ want to allow null selection, you might want to set a
  23. specific caption for the empty item by adding a “dummy” option to the
  24. list and setting it as the null selection item:
  25. [source,java]
  26. ....
  27. ComboBox cbExample = new ComboBox();
  28. cbExample.setNullSelectionAllowed(true);
  29. cbExample.addItem(“[ None ]”);
  30. cbExample.setNullSelectionItemId(“[ None ]”);
  31. ....
  32. Consider surrounding the null item’s caption with brackets or dashes to
  33. distinguish it from the other options, and make sure that it ends up in
  34. the beginning or end of the list.
  35. [[enable-text-input-only-when-appropriate]]
  36. Enable text input only when appropriate
  37. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  38. The Vaadin ComboBox can behave either as an actual combo box (i.e.
  39. combining a textfield with a dropdown list), but also as a normal
  40. dropdown menu, like the html `<select>` element without text entry.
  41. This is controlled through the `setTextInputAllowed()` method.
  42. Text input is great if
  43. 1. the list is very long, where using the text field as a filter helps in finding the correct item, or
  44. 2. the users need to be able to add new values to the list, in which case adding new items must also be enabled using `setNewItemsAllowed(true)`.
  45. [source,java]
  46. ....
  47. ComboBox cbEducation = new ComboBox(“Education”);
  48. cbEducation.setInputPrompt(“Choose degree from list or enter your own”);
  49. cbEducation.setTextInputAllowed(true);
  50. cbEducation.setNewItemsAllowed(true);
  51. ....
  52. If only items in the list can be chosen, and the list is quite short
  53. (say, less than 10 entries), it’s actually better to *disable text
  54. input*, because that makes the click target for opening the dropdown
  55. cover the entire field, instead of just the small arrow-icon/button at
  56. the end (dropdown menu click-target areas marked in pink below):
  57. image:img/combos-textinput.png[Text input]
  58. (Actually in these cases, you might want to consider *NativeSelect*,
  59. which is really just a wrapper around the normal html `<select>`
  60. element. But then you can’t have an input prompt or any of the other
  61. nice features of *ComboBox*.)