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

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