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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 with a [classname]#Property.ValueChangeListener#, as
  29. in most other fields. The value can be acquired with [methodname]#getValue()#
  30. directly from the text field, as is done in the example below, or from the
  31. property reference of the event.
  32. [source, java]
  33. ----
  34. // Handle changes in the value
  35. tf.addValueChangeListener(new Property.ValueChangeListener() {
  36. public void valueChange(ValueChangeEvent event) {
  37. // Assuming that the value type is a String
  38. String value = (String) event.getProperty().getValue();
  39. // Do something with the value
  40. Notification.show("Value is: " + value);
  41. }
  42. });
  43. // Fire value changes immediately when the field loses focus
  44. tf.setImmediate(true);
  45. ----
  46. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.textfield.inputhandling[on-line example, window="_blank"].
  47. As with other event listeners, you can use lambda expression with one parameter
  48. to handle the events in Java 8.
  49. Much of the API of [classname]#TextField# is defined in
  50. [classname]#AbstractTextField#, which allows different kinds of text input
  51. fields, such as rich text editors, which do not share all the features of the
  52. single-line text fields.
  53. [[figure.components.textfield.api]]
  54. .Text Field Class Relationships
  55. image::img/textfield-diagram-hi.png[width=40%, scaledwidth=70%]
  56. [[components.textfield.databinding]]
  57. == Data Binding
  58. [classname]#TextField# edits [classname]#String# values, but you can bind it to
  59. any property type that has a proper converter, as described in
  60. <<dummy/../../../framework/datamodel/datamodel-properties#datamodel.properties.converter,"Converting
  61. Between Property Type and Representation">>.
  62. [source, java]
  63. ----
  64. // Have an initial data model. As Double is unmodificable and
  65. // doesn't support assignment from String, the object is
  66. // reconstructed in the wrapper when the value is changed.
  67. Double trouble = 42.0;
  68. // Wrap it in a property data source
  69. final ObjectProperty<Double> property =
  70. new ObjectProperty<Double>(trouble);
  71. // Create a text field bound to it
  72. // (StringToDoubleConverter is used automatically)
  73. TextField tf = new TextField("The Answer", property);
  74. tf.setImmediate(true);
  75. // Show that the value is really written back to the
  76. // data source when edited by user.
  77. Label feedback = new Label(property);
  78. feedback.setCaption("The Value");
  79. ----
  80. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.textfield.databinding[on-line example, window="_blank"].
  81. When you put a [classname]#Table# in editable mode or create fields with a
  82. [classname]#FieldGroup#, the [classname]#DefaultFieldFactory# creates a
  83. [classname]#TextField# for almost every property type by default. You often need
  84. to make a custom factory to customize the creation and to set the field tooltip,
  85. validation, formatting, and so on.
  86. See
  87. <<dummy/../../../framework/datamodel/datamodel-overview.asciidoc#datamodel.overview,"Binding
  88. Components to Data">> for more details on data binding, field factories for
  89. [classname]#Table# in
  90. <<dummy/../../../framework/components/components-table#components.table.editing,"Editing
  91. the Values in a Table">>, and
  92. <<dummy/../../../framework/datamodel/datamodel-itembinding#datamodel.itembinding,"Creating
  93. Forms by Binding Fields to Items">> regarding forms.
  94. [[components.textfield.length]]
  95. == String Length
  96. The [methodname]#setMaxLength()# method sets the maximum length of the input
  97. string so that the browser prevents the user from entering a longer one. As a
  98. security feature, the input value is automatically truncated on the server-side,
  99. as the maximum length setting could be bypassed on the client-side. The maximum
  100. length property is defined at [classname]#AbstractTextField# level.
  101. Notice that the maximum length setting does not affect the width of the field.
  102. You can set the width with [methodname]#setWidth()#, as with other components.
  103. Using __em__ widths is recommended to better approximate the proper width in
  104. relation to the size of the used font, but the __em__ width is not exactly the
  105. width of a letter and varies by browser and operating system. There is no standard
  106. way in HTML for setting the width exactly to a number of letters (in a monospaced font).
  107. [[components.textfield.nullvalues]]
  108. == Handling Null Values
  109. ((("Null representation", id="term.components.textfield.nullvalues", range="startofrange")))
  110. ((("[methodname]#setNullRepresentation()#")))
  111. As with any field, the value of a [classname]#TextField# can be set as
  112. [parameter]#null#. This occurs most commonly when you create a new field without
  113. setting a value for it or bind the field value to a data source that allows null
  114. values. In such case, you might want to show a special value that stands for the
  115. null value. You can set the null representation with the
  116. [methodname]#setNullRepresentation()# method. Most typically, you use an empty
  117. string for the null representation, unless you want to differentiate from a
  118. string that is explicitly empty. The default null representation is "[literal]#null#", which essentially warns that you may have forgotten to
  119. initialize your data objects properly.
  120. ((("[methodname]#setNullSettingAllowed()#")))
  121. The [methodname]#setNullSettingAllowed()# controls whether the user can actually
  122. input a null value by using the null value representation. If the setting is
  123. [literal]#++false++#, which is the default, inputting the null value
  124. representation string sets the value as the literal value of the string, not
  125. null. This default assumption is a safeguard for data sources that may not allow
  126. null values.
  127. [source, java]
  128. ----
  129. // Have a property with null value
  130. ObjectProperty<Double> dataModel =
  131. new ObjectProperty<Double>(new Double(0.0));
  132. dataModel.setValue(null); // Have to set it null here
  133. // Create a text field bound to the null data
  134. TextField tf = new TextField("Field Energy (J)", dataModel);
  135. tf.setNullRepresentation("-- null-point --");
  136. // Allow user to input the null value by its representation
  137. tf.setNullSettingAllowed(true);
  138. ----
  139. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.textfield.nullvaluerepresentation[on-line example, window="_blank"].
  140. The [classname]#Label#, which is bound to the value of the
  141. [classname]#TextField#, displays a null value as empty. The resulting user
  142. interface is shown in <<figure.components.textfield.nullvalues>>.
  143. [[figure.components.textfield.nullvalues]]
  144. .Null Value Representation
  145. image::img/textfield-nullrepresentation.png[width=35%, scaledwidth=50%]
  146. (((range="endofrange", startref="term.components.textfield.nullvalues")))
  147. [[components.textfield.textchangeevents]]
  148. == Text Change Events
  149. ((("[classname]#Text change events#", id="term.components.textfield.textchangeevents", range="startofrange")))
  150. Often you want to receive a change event immediately when the text field value
  151. changes. The __immediate__ mode is not literally immediate, as the changes are
  152. transmitted only after the field loses focus. In the other extreme, using
  153. keyboard events for every keypress would make typing unbearably slow and also
  154. processing the keypresses is too complicated for most purposes. __Text change
  155. events__ are transmitted asynchronously soon after typing and do not block
  156. typing while an event is being processed.
  157. ((([classname]#TextChangeListener#)))
  158. Text change events are received with a [classname]#TextChangeListener#, as is
  159. done in the following example that demonstrates how to create a text length
  160. counter:
  161. [source, java]
  162. ----
  163. // Text field with maximum length
  164. final TextField tf = new TextField("My Eventful Field");
  165. tf.setValue("Initial content");
  166. tf.setMaxLength(20);
  167. // Counter for input length
  168. final Label counter = new Label();
  169. counter.setValue(tf.getValue().length() +
  170. " of " + tf.getMaxLength());
  171. // Display the current length interactively in the counter
  172. tf.addTextChangeListener(new TextChangeListener() {
  173. public void textChange(TextChangeEvent event) {
  174. int len = event.getText().length();
  175. counter.setValue(len + " of " + tf.getMaxLength());
  176. }
  177. });
  178. // The lazy mode is actually the default
  179. tf.setTextChangeEventMode(TextChangeEventMode.LAZY);
  180. ----
  181. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.textfield.textchangeevents.counter[on-line example, window="_blank"].
  182. The result is shown in <<figure.components.textfield.textchangeevents>>.
  183. [[figure.components.textfield.textchangeevents]]
  184. .Text Change Events
  185. image::img/textfield-textchangeevents.png[width=35%, scaledwidth=50%]
  186. The __text change event mode__ defines how quickly the changes are transmitted
  187. to the server and cause a server-side event. Lazier change events allow sending
  188. larger changes in one event if the user is typing fast, thereby reducing server
  189. requests.
  190. ((([classname]#TextChangeEventMode#)))
  191. You can set the text change event mode of a [classname]#TextField# with
  192. [methodname]#setTextChangeEventMode()#. The allowed modes are defined in
  193. [classname]#TextChangeEventMode# enum and are as follows:
  194. [parameter]#TextChangeEventMode.LAZY#(default):: An event is triggered when there is a pause in editing the text. The length of
  195. the pause can be modified with [methodname]#setInputEventTimeout()#. As with the
  196. [parameter]#TIMEOUT# mode, a text change event is forced before a possible
  197. [classname]#ValueChangeEvent#, even if the user did not keep a pause while
  198. entering the text.
  199. +
  200. This is the default mode.
  201. [parameter]#TextChangeEventMode.TIMEOUT#:: A text change in the user interface causes the event to be communicated to the
  202. application after a timeout period. If more changes are made during this period,
  203. the event sent to the server-side includes the changes made up to the last
  204. change. The length of the timeout can be set with
  205. [methodname]#setInputEventTimeout()#.
  206. +
  207. If a [classname]#ValueChangeEvent# would occur before the timeout period, a
  208. [classname]#TextChangeEvent# is triggered before it, on the condition that the
  209. text content has changed since the previous [classname]#TextChangeEvent#.
  210. [parameter]#TextChangeEventMode.EAGER#:: An event is triggered immediately for every change in the text content,
  211. typically caused by a key press. The requests are separate and are processed
  212. sequentially one after another. Change events are nevertheless communicated
  213. asynchronously to the server, so further input can be typed while event requests
  214. are being processed.
  215. (((range="endofrange", startref="term.components.textfield.textchangeevents")))
  216. [[components.textfield.css]]
  217. == CSS Style Rules
  218. [source, css]
  219. ----
  220. .v-textfield { }
  221. ----
  222. The HTML structure of [classname]#TextField# is extremely simple, consisting
  223. only of an element with the [literal]#++v-textfield++# style.
  224. For example, the following custom style uses dashed border:
  225. [source, css]
  226. ----
  227. .v-textfield-dashing {
  228. border: thin dashed;
  229. background: white; /* Has shading image by default */
  230. }
  231. ----
  232. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.textfield.css[on-line example, window="_blank"].
  233. The result is shown in <<figure.components.textfield.css>>.
  234. [[figure.components.textfield.css]]
  235. .Styling TextField with CSS
  236. image::img/textfield-css.png[]
  237. The style name for [classname]#TextField# is also used in several components
  238. that contain a text input field, even if the text input is not an actual
  239. [classname]#TextField#. This ensures that the style of different text input
  240. boxes is similar.
  241. (((range="endofrange", startref="term.components.textfield")))