2

Is it possible to draw html contents in vaadin label?

I have texts with <br> contents. Is it available to let the label to use them?

StoopidDonut
  • 8,547
  • 2
  • 33
  • 51
chabeee
  • 305
  • 3
  • 14

2 Answers2

8

Yes, you can use the 2 argument constructor of Label and set the ContentMode to HTML.

Something like,

new Label(YOUR_HTML_TEXT, ContentMode.HTML);

From the Javadoc (linked above):

Label component for showing non-editable short texts. The label content can be set to the modes specified by ContentMode

The contents of the label may contain simple formatting:

  • Bold
  • Italic
  • Underlined
  • Linebreak
  • ...
StoopidDonut
  • 8,547
  • 2
  • 33
  • 51
  • 4
    please be aware, that the documentation suggest, that some sanitation is done here, but it isn't. you can e.g. use ``new Label('HelloWorld', ContentMode.HTML)`` and with this content beeing e.g. provided by user input creates a security problem. – cfrick Jun 30 '14 at 20:11
1
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Label;

Label htmlLabel = new Label("<h1>Header</h1><strike>text</strike>");
htmlLabel.setContentMode(ContentMode.HTML);

This piece of code shows needed classes and calls to have HTML markup rendered properly in a vaadin's Label.

dimirsen Z
  • 873
  • 13
  • 16