Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

components-textfield.asciidoc 6.5KB

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