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-label.asciidoc 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. ---
  2. title: Label
  3. order: 7
  4. layout: page
  5. ---
  6. [[components.label]]
  7. = [classname]#Label#
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/data-presentation/label"]
  11. endif::web[]
  12. [classname]#Label# component displays non-editable text. This text can be used
  13. for short simple labels or for displaying long text, such as paragraphs. The
  14. text can be formatted in HTML or as preformatted text, depending on the
  15. __content mode__ of the label.
  16. You can give the label text most conviniently in the constructor, as is done in
  17. the following. Label has 100% default width, so the containing layout must also
  18. have defined width.
  19. [source, java]
  20. ----
  21. // A container that is 100% wide by default
  22. VerticalLayout layout = new VerticalLayout();
  23. Label label = new Label("Labeling can be dangerous");
  24. layout.addComponent(label);
  25. ----
  26. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.label.basic[on-line example, window="_blank"].
  27. [classname]#Label# implements the [interfacename]#Property# interface to allow
  28. accessing the text value, so you can get and set the text with
  29. [methodname]#getValue()# and [methodname]#setValue()#.
  30. [source, java]
  31. ----
  32. // Get the label's text to initialize a field
  33. TextField editor = new TextField(null, // No caption
  34. label.getValue());
  35. // Change the label's text
  36. editor.addValueChangeListener(event -> // Java 8
  37. label.setValue(editor.getValue()));
  38. editor.setImmediate(true); // Send on Enter
  39. ----
  40. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.label.basic[on-line example, window="_blank"].
  41. Label also supports data binding to a property data source, as described later
  42. in <<components.label.databinding>>. However, in that case the value can not be
  43. set through the label, as [classname]#Label# is not a
  44. [interfacename]#Property.Editor# and is not allowed to write to a bound
  45. property.
  46. Even though [classname]#Label# is text and is often used as a caption, it is a
  47. normal component and therefore also has a caption that you can set with
  48. [methodname]#setCaption()#. As with most other components, the caption is
  49. managed by the containing layout.
  50. [[components.label.wrap]]
  51. == Text Width and Wrapping
  52. [classname]#Label# has 100% default width, so the containing layout must also
  53. have a defined width. If the width of the label's text exceeds the width of the
  54. label, the text will wrap around and continue on the next line. Some layout
  55. components have undefined width by default, such as
  56. [classname]#HorizontalLayout#, so you need to pay special care with them.
  57. [source, java]
  58. ----
  59. // A container with a defined width.
  60. Panel panel = new Panel("Panel Containing a Label");
  61. panel.setWidth("300px");
  62. panel.setContent(
  63. new Label("This is a Label inside a Panel. There is " +
  64. "enough text in the label to make the text " +
  65. "wrap when it exceeds the width of the panel."));
  66. ----
  67. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.label.wrap[on-line example, window="_blank"].
  68. As the size of the [classname]#Panel# in the above example is fixed and the
  69. width of [classname]#Label# is the default 100%, the text in the
  70. [classname]#Label# will wrap to fit the panel, as shown in
  71. <<figure.components.label>>.
  72. [[figure.components.label]]
  73. .The Label Component
  74. image::img/label-example1.png[width=50%, scaledwidth=75%]
  75. Setting [classname]#Label# to undefined width will cause it to not wrap at the
  76. end of the line, as the width of the content defines the width. If placed inside
  77. a layout with defined width, the [classname]#Label# will overflow the layout
  78. horizontally and, normally, be truncated.
  79. [[components.label.content-mode]]
  80. == Content Mode
  81. The content of a label is formatted depending on a __content mode__. By default,
  82. the text is assumed to be plain text and any contained XML-specific characters
  83. will be quoted appropriately to allow rendering the contents of a label in HTML
  84. in a web browser. The content mode can be set in the constructor or with
  85. [methodname]#setContentMode()#, and can have the values defined in the
  86. [classname]#ContentMode# enumeration type in
  87. [package]#com.vaadin.shared.ui.label# package:
  88. TEXT:: The default content mode where the label contains only plain text. All
  89. characters are allowed, including the special [literal]#++<++#,
  90. [literal]#++>++#, and [literal]#++&++# characters in XML or HTML, which are
  91. quoted properly in HTML while rendering the component. This is the default mode.
  92. PREFORMATTED:: Content mode where the label contains preformatted text. It will be, by default,
  93. rendered with a fixed-width typewriter font. Preformatted text can contain line
  94. breaks, written in Java with the [literal]#++\n++# escape sequence for a newline
  95. character (ASCII 0x0a), or tabulator characters written with [literal]#++\t++#
  96. (ASCII 0x09).
  97. HTML:: Content mode where the label contains HTML.
  98. +
  99. Please note the following security and validity warnings regarding the HTML
  100. content mode.
  101. [WARNING]
  102. .Cross-Site Scripting Warning
  103. ====
  104. Having [classname]#Label# in HTML content mode allows pure HTML content. If the
  105. content comes from user input, you should always carefully sanitize it to
  106. prevent cross-site scripting (XSS) attacks. Please see
  107. <<dummy/../../../framework/advanced/advanced-security#advanced.security.sanitizing,"Sanitizing
  108. User Input to Prevent Cross-Site Scripting">>.
  109. Also, the validity of the HTML content is not checked when rendering the
  110. component and any errors can result in an error in the browser. If the content
  111. comes from an uncertain source, you should always validate it before displaying
  112. it in the component.
  113. ====
  114. The following example demonstrates the use of [classname]#Label# in different
  115. modes.
  116. [source, java]
  117. ----
  118. Label textLabel = new Label(
  119. "Text where formatting characters, such as \\n, " +
  120. "and HTML, such as <b>here</b>, are quoted.",
  121. ContentMode.TEXT);
  122. Label preLabel = new Label(
  123. "Preformatted text is shown in an HTML <pre> tag.\n" +
  124. "Formatting such as\n" +
  125. " * newlines\n" +
  126. " * whitespace\n" +
  127. "and such are preserved. HTML tags, \n"+
  128. "such as <b>bold</b>, are quoted.",
  129. ContentMode.PREFORMATTED);
  130. Label htmlLabel = new Label(
  131. "In HTML mode, all HTML formatting tags, such as \n" +
  132. "<ul>"+
  133. " <li><b>bold</b></li>"+
  134. " <li>itemized lists</li>"+
  135. " <li>etc.</li>"+
  136. "</ul> "+
  137. "are preserved.",
  138. ContentMode.HTML);
  139. ----
  140. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.label.content-modes.contentmodes[on-line example, window="_blank"].
  141. The rendering will look as shown in <<figure.components.label.content-mode>>.
  142. [[figure.components.label.content-mode]]
  143. .Label Content Modes
  144. image::img/label-modes.png[width=75%, scaledwidth=100%]
  145. ifdef::web[]
  146. [[components.label.spacing]]
  147. == Spacing with a [classname]#Label#
  148. You can use a [classname]#Label# to create vertical or horizontal space in a
  149. layout. If you need a empty "line" in a vertical layout, having just a label
  150. with empty text is not enough, as it will collapse to zero height. The same goes
  151. for a label with only whitespace as the label text. You need to use a
  152. non-breaking space character, either [literal]#++&nbsp;++# or
  153. [literal]#++&#160;++#:
  154. [source, java]
  155. ----
  156. layout.addComponent(new Label("&nbsp;", ContentMode.HTML));
  157. ----
  158. Using the [parameter]#ContentMode.PREFORMATTED# mode has the same effect;
  159. preformatted spaces do not collapse in a vertical layout. In a
  160. [classname]#HorizontalLayout#, the width of a space character may be
  161. unpredictable if the label font is proportional, so you can use the preformatted
  162. mode to add em-width wide spaces.
  163. If you want a gap that has adjustable width or height, you can use an empty
  164. label if you specify a height or width for it. For example, to create vertical
  165. space in a [classname]#VerticalLayout#:
  166. [source, java]
  167. ----
  168. Label gap = new Label();
  169. gap.setHeight("1em");
  170. verticalLayout.addComponent(gap);
  171. ----
  172. You can make a flexible expanding spacer by having a relatively sized empty
  173. label with [literal]#++100%++# height or width and setting the label as
  174. expanding in the layout.
  175. [source, java]
  176. ----
  177. // A wide component bar
  178. HorizontalLayout horizontal = new HorizontalLayout();
  179. horizontal.setWidth("100%");
  180. // Have a component before the gap (a collapsing cell)
  181. Button button1 = new Button("I'm on the left");
  182. horizontal.addComponent(button1);
  183. // An expanding gap spacer
  184. Label expandingGap = new Label();
  185. expandingGap.setWidth("100%");
  186. horizontal.addComponent(expandingGap);
  187. horizontal.setExpandRatio(expandingGap, 1.0f);
  188. // A component after the gap (a collapsing cell)
  189. Button button2 = new Button("I'm on the right");
  190. horizontal.addComponent(button2);
  191. ----
  192. endif::web[]
  193. [[components.label.databinding]]
  194. == Data Binding
  195. While [classname]#Label# is not a field component, it is a
  196. [interfacename]#Property.Viewer# and can be bound to a property data source,
  197. described in
  198. <<dummy/../../../framework/datamodel/datamodel-properties#datamodel.properties,"Properties">>.
  199. You can specify the data source either in the constructor or by the
  200. [methodname]#setPropertyDataSource()# method.
  201. [source, java]
  202. ----
  203. // Some property
  204. ObjectProperty<String> property =
  205. new ObjectProperty<String>("some value");
  206. // Label that is bound to the property
  207. Label label = new Label(property);
  208. ----
  209. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.label.binding[on-line example, window="_blank"].
  210. Further, as [classname]#Label# is a [interfacename]#Property#, you can edit its
  211. value with a property editor, such as a field:
  212. [source, java]
  213. ----
  214. Label label = new Label("some value");
  215. TextField editor = new TextField();
  216. editor.setPropertyDataSource(label);
  217. editor.setImmediate(true);
  218. ----
  219. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.label.delegation[on-line example, window="_blank"].
  220. However, [classname]#Label# is __not__ a [interfacename]#Property.Editor#, so it
  221. is read-only when bound to a data source. Therefore, you can not use
  222. [methodname]#setValue()# to set the value of a connected data source through a
  223. [classname]#Label# nor bind the label to an editor field, in which case writes
  224. would be delegated through the label.
  225. [[components.label.css]]
  226. == CSS Style Rules
  227. [source, css]
  228. ----
  229. .v-label { }
  230. pre { } /* In PREFORMATTED content mode */
  231. ----
  232. The [classname]#Label# component has a [literal]#++v-label++# overall style. In
  233. the [parameter]#PREFORMATTED# content mode, the text is wrapped inside a
  234. [literal]#++<pre>++# element.