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

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