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

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