Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT label with line breaks

Tags:

GWT Label widgets iterprets everything as text, not as html tags - that's good, but I would like it to interpret \n as a <br /> how do i do that.

I would make subclass, but I cant find what to override to achieve this behaviour

(I could use HTML widget, but it would interpret all tags - and all I need is an line brak)

like image 581
Hurda Avatar asked May 31 '11 08:05

Hurda


People also ask

How do you break a line in a label in HTML?

The <br> HTML element produces a line break in text (carriage-return).

How do you insert a break in line?

To add spacing between lines or paragraphs of text in a cell, use a keyboard shortcut to add a new line. Click the location where you want to break the line. Press ALT+ENTER to insert the line break.

How do I create an auto line break in HTML?

The <br> tag inserts a single line break. The <br> tag is useful for writing addresses or poems.


2 Answers

Use an HTML widget and set its value using a SafeHtml constructed with SafeHtmlBuilder.appendEscapedLines:

HTML label = new HTML(new SafeHtmlBuilder().appendEscapedLines("foo<bar\nbaz>quux").toSafeHtml()); 

(alternatively, you can split("\n", -1) your text, call SafeHtml.htmlEscape on each part and join them back with a <br>, that's what appendEscapedLines does)

like image 79
Thomas Broyer Avatar answered Sep 21 '22 13:09

Thomas Broyer


Another option is to use CSS, which may be sufficient in some cases where this problem emerges.

Add the CSS attribute white-space: pre or pre-wrap in the area where you display the text. It will ensure that line breaks are respected when rendering the document.

This approach has the potential to reduce some complexity, e.g. the processing of input where \n is replaced with <br/>.

like image 25
Henrik Aasted Sørensen Avatar answered Sep 19 '22 13:09

Henrik Aasted Sørensen