Vaadin: My shortcut ignores the carriage return character

I have an incoming text string containing a line break ("\ r").

When I output it using: System.out.println(myString) , the carriage return is interpreted.

However, when I set the line as the contents of the label, it ignores the carriage return.

How can I make Label interpret carriage return / line return (without XHTML mode)?

+4
source share
3 answers

Here's how you can put this text in your shortcut:

 @Override public void init() { Window window = new Window(); Label label = new Label("<pre>First line\rSecond line\nThird line</pre>", Label.CONTENT_XHTML); window.addComponent(label); setMainWindow(window); } 

The key uses the Label.CONTENT_XHTML content mode and includes the text inside the <pre> .

+6
source

In Vaadin 7.0, you can use ContentMode.PREFORMATTED, for example:

 String resultText = "First line\rSecond line\nThird line"; Label dateLabel = new Label( resultText, ContentMode.PREFORMATTED ); 

and if you want the text to look sexy, you can use some themes, for example:

 dateLabel.setStyleName( Runo.LABEL_SMALL ); 

It should work and be elegant.

+2
source

After reading Vaadin’s book and several tests, I don’t think that \ r can be interpreted as a shortcut.

Replacing \ r with \ n gives you two options:

 Label.setContentMode(Label.CONTENT_XHTML). //But you don't want to do this Label.setContentMode(Label.CONTENT_PREFORMATTED) //But I think it not the display you want 

Sincerely.

Éric

0
source

Source: https://habr.com/ru/post/1390406/


All Articles