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.

ReadOnlyVsDisabledFields.asciidoc 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. ---
  2. title: Read Only Vs Disabled Fields
  3. order: 56
  4. layout: page
  5. ---
  6. [NOTE]
  7. *Partially outdated*:
  8. This article was written before Vaadin 7.3, and is referring to the previous default theme, Reindeer. The visual appearance of read-only fields is different in the newer default theme, Valo. In Valo, read-only fields retain the border around the input field.
  9. [[read-only-vs-disabled-fields]]
  10. Read-only vs Disabled fields
  11. ----------------------------
  12. Vaadin input field components have both a _disabled_ and a _read-only_
  13. state (which can be set through the *setEnabled(false)* and
  14. *setReadOnly(true)* methods respectively). While at first glance these
  15. seem to be interchangeable, there is a distinct difference in their
  16. effects and proper usage.
  17. image:img/disabledvsreadonly.png[Disabled vs read-only]
  18. As you can see in the image above, the visual effects of the two states
  19. are clearly different. The _disabled_ ComboBox is “grayed out” but looks
  20. otherwise identical to its normal state. The _read-only_ ComboBox,
  21. meanwhile, doesn’t look like a ComboBox at all, but more like a *Label*.
  22. This visual difference is the key to understanding when to use which
  23. state.
  24. [[disabled-fields]]
  25. Disabled fields
  26. ^^^^^^^^^^^^^^^
  27. An input field is disabled to indicate that it cannot _currently_ be
  28. used. This might be because it is _not applicable_, e.g. due to the
  29. value of some other field. Let’s look at the following piece of a
  30. questionnaire form as an example:
  31. image:img/disabled1.png[Disabled example]
  32. The “years” dropdown above is _disabled_ unless the _Yes_ radio button
  33. is selected, since it naturally _is not applicable_ otherwise. Another
  34. reason for disabling a field might be that the user lacks the required
  35. permissions to set it, such as in the following example:
  36. image:img/disabled3.png[Disabled example 2]
  37. In both cases, there is no need to _read_ the value of the field, since
  38. it cannot have a value or simply isn’t applicable or relevant in the
  39. current context. This is why disabled fields are grayed out with a
  40. reduced opacity effect in Vaadin built-in themes.
  41. [[read-only-fields]]
  42. Read-only fields
  43. ^^^^^^^^^^^^^^^^
  44. _Read-only_, on the other hand, is used when a field is currently only
  45. used to _display_ a value, without providing any means of changing it.
  46. In this case, it is important that the field is presented in a
  47. _readable_ way, which is why Vaadin generally renders them like labels,
  48. without the unnecessary component chrome. A very common example is a
  49. form that can be toggled between viewing and editing modes:
  50. image:img/viewmode-readonly.png[Viewing vs editing]
  51. Using read-only fields in viewing mode means that you don’t have to swap
  52. between labels and input fields in your UI code when view/edit mode is
  53. toggled. Instead, you just iterate through your fields set read-only
  54. mode on or off:
  55. [source,java]
  56. ....
  57. Iterator<Component> i = someLayout.getComponentIterator();
  58. while (i.hasNext()) {
  59. Component c = i.next();
  60. if (c instanceof com.vaadin.ui.AbstractField) {
  61. AbstractField field = (AbstractField)c;
  62. field.setReadOnly(true);
  63. }
  64. }
  65. ....
  66. Even better, if your fields are databound through a *FieldGroup*, their
  67. read-only states can be collectively toggled through the *FieldGroup*:
  68. [source,java]
  69. ....
  70. FieldGroup fieldGrp = new FieldGroup(dataItem);
  71. TextField tfName = new TextField(“Name”);
  72. fieldGrp.bind(tfName, “name”);
  73. TextField tfAge = new TextField(“Age”);
  74. fieldGrp.bind(tfAge, “age”);
  75. fieldGrp.setReadOnly(true);
  76. ....
  77. (Unfortunately, setting a Vaadin component container, like a layout,
  78. _read-only_ does not set all its components read-only recursively, as
  79. one might expect. Doing the same with _disabled does_, though.)
  80. One caveat regarding read-only fields is that if the text is longer than
  81. the field, it will be clipped, as opposed to a Label, which instead will
  82. wrap the text to a new line. Thus, in certain situations, switching to
  83. Labels may be preferable.
  84. It’s probably best to mention here that *setReadOnly(true)* also has a
  85. certain side-effect in Vaadin that *setEnabled(false)* does not: You
  86. cannot set the value of a _read-only_ field even in server-side code.
  87. The following code would throw a *ReadOnlyException*:
  88. [source,java]
  89. ....
  90. TextField tfFoo = new TextField();
  91. tfFoo.setReadOnly(true);
  92. tfFoo.setValue(“foo”);
  93. ....
  94. [[why-this-is-important]]
  95. Why this is important
  96. ^^^^^^^^^^^^^^^^^^^^^
  97. Understanding the difference between _disabled_ and _read-only_ is
  98. important because using them wrong tends to harm usability. First, let’s
  99. see what happens if you use _disabled_ instead _read-only_ in the
  100. view/edit example form above:
  101. image:img/viewmode-disabled.png[Viewing with disabled]
  102. Not very readable, is it? Looks kind of awful, doesn’t it? The reduced
  103. opacity and unnecessary component chrome really make _reading_ the form
  104. rather painful, which kind of defeats the entire "view" mode.
  105. Okay, so the other way around, what if we use _read-only_ instead of
  106. _disabled_ in the wrong situation? The field will look like a *Label*,
  107. and that’s not so bad, right? Well, if the field is _empty_, as is often
  108. the case with disabled fields, then in read-only mode it would simply be
  109. _invisible_, save for the caption (if it has one):
  110. image:img/readonly-wrong.png[Readonly wrong use]
  111. Admittedly, not as bad as using disabled for read-only forms, but bad
  112. enough. Also, even if the field does have a value, setting it
  113. _read-only_ gives the user the impression that it cannot be changed at
  114. all, whereas a grayed out _disabled_ field indicates that it could be
  115. changed, if only the circumstances were different...