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

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