Label

Live Demo

Label component displays non-editable text. This text can be used for short simple labels or for displaying long text, such as paragraphs. The text can be formatted in HTML or as preformatted text, depending on the content mode of the label.

You can give the label text most conviniently in the constructor, as is done in the following. Label has 100% default width, so the containing layout must also have defined width.

Java

  1. // A container that is 100% wide by default
  2. VerticalLayout layout = new VerticalLayout();
  3. Label label = new Label("Labeling can be dangerous");
  4. layout.addComponent(label);

See the on-line example.

Label implements the Property interface to allow accessing the text value, so you can get and set the text with getValue() and setValue().

Java

  1. // Get the label's text to initialize a field
  2. TextField editor = new TextField(null, // No caption
  3. label.getValue());
  4. // Change the label's text
  5. editor.addValueChangeListener(event -> // Java 8
  6. label.setValue(editor.getValue()));
  7. editor.setImmediate(true); // Send on Enter

See the on-line example.

Label also supports data binding to a property data source, as described later in Data Binding. However, in that case the value can not be set through the label, as Label is not a Property.Editor and is not allowed to write to a bound property.

Even though Label is text and is often used as a caption, it is a normal component and therefore also has a caption that you can set with setCaption(). As with most other components, the caption is managed by the containing layout.

Text Width and Wrapping

Label has 100% default width, so the containing layout must also have a defined width. If the width of the label’s text exceeds the width of the label, the text will wrap around and continue on the next line. Some layout components have undefined width by default, such as HorizontalLayout, so you need to pay special care with them.

Java

  1. // A container with a defined width.
  2. Panel panel = new Panel("Panel Containing a Label");
  3. panel.setWidth("300px");
  4. panel.setContent(
  5. new Label("This is a Label inside a Panel. There is " +
  6. "enough text in the label to make the text " +
  7. "wrap when it exceeds the width of the panel."));

See the on-line example.

As the size of the Panel in the above example is fixed and the width of Label is the default 100%, the text in the Label will wrap to fit the panel, as shown in The Label Component.

label example1

The Label Component

Setting Label to undefined width will cause it to not wrap at the end of the line, as the width of the content defines the width. If placed inside a layout with defined width, the Label will overflow the layout horizontally and, normally, be truncated.

Content Mode

The content of a label is formatted depending on a content mode. By default, the text is assumed to be plain text and any contained XML-specific characters will be quoted appropriately to allow rendering the contents of a label in HTML in a web browser. The content mode can be set in the constructor or with setContentMode(), and can have the values defined in the ContentMode enumeration type in com.vaadin.shared.ui.label package:

TEXT

The default content mode where the label contains only plain text. All characters are allowed, including the special <, >, and & characters in XML or HTML, which are quoted properly in HTML while rendering the component. This is the default mode.

PREFORMATTED

Content mode where the label contains preformatted text. It will be, by default, rendered with a fixed-width typewriter font. Preformatted text can contain line breaks, written in Java with the \n escape sequence for a newline character (ASCII 0x0a), or tabulator characters written with \t (ASCII 0x09).

HTML

Content mode where the label contains HTML.

Please note the following security and validity warnings regarding the HTML content mode.

Warning
Cross-Site Scripting Warning

Having Label in HTML content mode allows pure HTML content. If the content comes from user input, you should always carefully sanitize it to prevent cross-site scripting (XSS) attacks. Please see “Sanitizing User Input to Prevent Cross-Site Scripting”.

Also, the validity of the HTML content is not checked when rendering the component and any errors can result in an error in the browser. If the content comes from an uncertain source, you should always validate it before displaying it in the component.

The following example demonstrates the use of Label in different modes.

Java

  1. Label textLabel = new Label(
  2. "Text where formatting characters, such as \\n, " +
  3. "and HTML, such as <b>here</b>, are quoted.",
  4. ContentMode.TEXT);
  5. Label preLabel = new Label(
  6. "Preformatted text is shown in an HTML <pre> tag.\n" +
  7. "Formatting such as\n" +
  8. " * newlines\n" +
  9. " * whitespace\n" +
  10. "and such are preserved. HTML tags, \n"+
  11. "such as <b>bold</b>, are quoted.",
  12. ContentMode.PREFORMATTED);
  13. Label htmlLabel = new Label(
  14. "In HTML mode, all HTML formatting tags, such as \n" +
  15. "<ul>"+
  16. " <li><b>bold</b></li>"+
  17. " <li>itemized lists</li>"+
  18. " <li>etc.</li>"+
  19. "</ul> "+
  20. "are preserved.",
  21. ContentMode.HTML);

See the on-line example.

The rendering will look as shown in Label Content Modes.

label modes

Label Content Modes

Spacing with a Label

You can use a Label to create vertical or horizontal space in a layout. If you need a empty “line” in a vertical layout, having just a label with empty text is not enough, as it will collapse to zero height. The same goes for a label with only whitespace as the label text. You need to use a non-breaking space character, either   or  :

Java

  1. layout.addComponent(new Label("&nbsp;", ContentMode.HTML));

Using the ContentMode.PREFORMATTED mode has the same effect; preformatted spaces do not collapse in a vertical layout. In a HorizontalLayout, the width of a space character may be unpredictable if the label font is proportional, so you can use the preformatted mode to add em-width wide spaces.

If you want a gap that has adjustable width or height, you can use an empty label if you specify a height or width for it. For example, to create vertical space in a VerticalLayout:

Java

  1. Label gap = new Label();
  2. gap.setHeight("1em");
  3. verticalLayout.addComponent(gap);

You can make a flexible expanding spacer by having a relatively sized empty label with 100% height or width and setting the label as expanding in the layout.

Java

  1. // A wide component bar
  2. HorizontalLayout horizontal = new HorizontalLayout();
  3. horizontal.setWidth("100%");
  4. // Have a component before the gap (a collapsing cell)
  5. Button button1 = new Button("I'm on the left");
  6. horizontal.addComponent(button1);
  7. // An expanding gap spacer
  8. Label expandingGap = new Label();
  9. expandingGap.setWidth("100%");
  10. horizontal.addComponent(expandingGap);
  11. horizontal.setExpandRatio(expandingGap, 1.0f);
  12. // A component after the gap (a collapsing cell)
  13. Button button2 = new Button("I'm on the right");
  14. horizontal.addComponent(button2);

Data Binding

While Label is not a field component, it is a Property.Viewer and can be bound to a property data source, described in “Properties”. You can specify the data source either in the constructor or by the setPropertyDataSource() method.

Java

  1. // Some property
  2. ObjectProperty<String> property =
  3. new ObjectProperty<String>("some value");
  4. // Label that is bound to the property
  5. Label label = new Label(property);

See the on-line example.

Further, as Label is a Property, you can edit its value with a property editor, such as a field:

Java

  1. Label label = new Label("some value");
  2. TextField editor = new TextField();
  3. editor.setPropertyDataSource(label);
  4. editor.setImmediate(true);

See the on-line example.

However, Label is not a Property.Editor, so it is read-only when bound to a data source. Therefore, you can not use setValue() to set the value of a connected data source through a Label nor bind the label to an editor field, in which case writes would be delegated through the label.

CSS Style Rules

CSS

  1. .v-label { }
  2. pre { } /* In PREFORMATTED content mode */

The Label component has a v-label overall style. In the PREFORMATTED content mode, the text is wrapped inside a <pre> element.