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.

components-textfield.asciidoc 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. ---
  2. title: TextField
  3. order: 9
  4. layout: page
  5. ---
  6. [[components.textfield]]
  7. = [classname]#TextField#
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/data-input/text-input/text-field"]
  11. endif::web[]
  12. ((("[classname]#TextField#", id="term.components.textfield", range="startofrange")))
  13. [classname]#TextField# is one of the most commonly used user interface components.
  14. It is a [classname]#Field# component that allows entering textual values with keyboard.
  15. The following example creates a simple text field:
  16. [source, java]
  17. ----
  18. // Create a text field
  19. TextField tf = new TextField("A Field");
  20. // Put some initial content in it
  21. tf.setValue("Stuff in the field");
  22. ----
  23. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.textfield.basic[on-line example, window="_blank"].
  24. The result is shown in <<figure.components.textfield.basic>>.
  25. [[figure.components.textfield.basic]]
  26. .[classname]#TextField# Example
  27. image::img/textfield-example.png[width=40%, scaledwidth=50%]
  28. Value changes are handled by adding a listener using [methodname]#addValueChangeListener()# method, as in most other fields. The value can be acquired with [methodname]#getValue()# directly from the text field or from the parameter
  29. passed to the event listener.
  30. [source, java]
  31. ----
  32. // Handle changes in the value
  33. tf.addValueChangeListener(event ->
  34. // Do something with the value
  35. Notification.show("Value is: " + event.getValue()));
  36. ----
  37. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.textfield.inputhandling[on-line example, window="_blank"].
  38. [classname]#TextField# edits [classname]#String# values, but you can use [classname]#Binder#
  39. to bind it to any property type that has a proper converter, as described in
  40. <<dummy/../../../framework/datamodel/datamodel-forms.asciidoc#datamodel.forms.conversion,"Conversion">>.
  41. Much of the API of [classname]#TextField# is defined in
  42. [classname]#AbstractTextField#, which allows different kinds of text input
  43. fields, which do not share all the features of the single-line text fields.
  44. [[figure.components.textfield.api]]
  45. .Text field class relationships
  46. image::img/textfield-diagram-hi.png[width=80%, scaledwidth=100%]
  47. [[components.textfield.length]]
  48. == String Length
  49. The [methodname]#setMaxLength()# method sets the maximum length of the input
  50. string so that the browser prevents the user from entering a longer one. As a
  51. security feature, the input value is automatically truncated on the server-side,
  52. as the maximum length setting could be bypassed on the client-side. The maximum
  53. length property is defined at [classname]#AbstractTextField# level.
  54. Notice that the maximum length setting does not affect the width of the field.
  55. You can set the width with [methodname]#setWidth()#, as with other components.
  56. Using __em__ widths is recommended to better approximate the proper width in
  57. relation to the size of the used font, but the __em__ width is not exactly the
  58. width of a letter and varies by browser and operating system. There is no standard
  59. way in HTML for setting the width exactly to a number of letters (in a monospaced font).
  60. [[components.textfield.textchangeevents]]
  61. == Configuring the Granularity of Value Change Events
  62. ((("[classname]#Text change events#", id="term.components.textfield.textchangeevents", range="startofrange")))
  63. Often you want to control how frequently [classname]#TextField# value changes are transmitted to the server.
  64. Sometimes the changes should be sent only after the field loses focus.
  65. In the other extreme, it can sometimes be useful to receive events every time the user presses a key.
  66. The __value change event mode__ defines how quickly the changes are transmitted
  67. to the server and cause a server-side event. Lazier change events allow sending
  68. larger changes in one event if the user is typing fast, thereby reducing server
  69. requests.
  70. ((([classname]#TextChangeEventMode#)))
  71. You can set the text change event mode of a [classname]#TextField# with
  72. [methodname]#setTextChangeEventMode()#. The allowed modes are defined in
  73. [classname]#TextChangeEventMode# enum and are as follows:
  74. [parameter]#TextChangeEventMode.LAZY#(default):: An event is triggered when there is a pause in editing the text. The length of
  75. the pause can be modified with [methodname]#setInputEventTimeout()#. As with the
  76. [parameter]#TIMEOUT# mode, a text change event is forced before a possible
  77. [classname]#ValueChangeEvent#, even if the user did not keep a pause while
  78. entering the text.
  79. +
  80. This is the default mode.
  81. [parameter]#TextChangeEventMode.TIMEOUT#:: A text change in the user interface causes the event to be communicated to the
  82. application after a timeout period. If more changes are made during this period,
  83. the event sent to the server-side includes the changes made up to the last
  84. change. The length of the timeout can be set with
  85. [methodname]#setInputEventTimeout()#.
  86. +
  87. If a [classname]#ValueChangeEvent# would occur before the timeout period, a
  88. [classname]#TextChangeEvent# is triggered before it, on the condition that the
  89. text content has changed since the previous [classname]#TextChangeEvent#.
  90. [parameter]#TextChangeEventMode.EAGER#:: An event is triggered immediately for every change in the text content,
  91. typically caused by a key press. The requests are separate and are processed
  92. sequentially one after another. Change events are nevertheless communicated
  93. asynchronously to the server, so further input can be typed while event requests
  94. are being processed.
  95. [source, java]
  96. ----
  97. // Text field with maximum length
  98. TextField tf = new TextField("My Eventful Field");
  99. tf.setValue("Initial content");
  100. tf.setMaxLength(20);
  101. // Counter for input length
  102. Label counter = new Label();
  103. counter.setValue(tf.getValue().length() +
  104. " of " + tf.getMaxLength());
  105. // Display the current length interactively in the counter
  106. tf.addValueChangeListener(event -> {
  107. int len = event.getValue().length();
  108. counter.setValue(len + " of " + tf.getMaxLength());
  109. });
  110. tf.setValueChangeMode(ValueChangeMode.onKeyPress());
  111. ----
  112. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.textfield.textchangeevents.counter[on-line example, window="_blank"].
  113. The result is shown in <<figure.components.textfield.textchangeevents>>.
  114. [[figure.components.textfield.textchangeevents]]
  115. .Text Change Events
  116. image::img/textfield-textchangeevents.png[width=35%, scaledwidth=50%]
  117. (((range="endofrange", startref="term.components.textfield.textchangeevents")))
  118. [[components.textfield.css]]
  119. == CSS Style Rules
  120. [source, css]
  121. ----
  122. .v-textfield { }
  123. ----
  124. The HTML structure of [classname]#TextField# is extremely simple, consisting
  125. only of an element with the [literal]#++v-textfield++# style.
  126. For example, the following custom style uses dashed border:
  127. [source, css]
  128. ----
  129. .v-textfield-dashing {
  130. border: thin dashed;
  131. background: white; /* Has shading image by default */
  132. }
  133. ----
  134. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.textfield.css[on-line example, window="_blank"].
  135. The result is shown in <<figure.components.textfield.css>>.
  136. [[figure.components.textfield.css]]
  137. .Styling TextField with CSS
  138. image::img/textfield-css.png[]
  139. The style name for [classname]#TextField# is also used in several components
  140. that contain a text input field, even if the text input is not an actual
  141. [classname]#TextField#. This ensures that the style of different text input
  142. boxes is similar.
  143. (((range="endofrange", startref="term.components.textfield")))