简体   繁体   中英

How to add text to new line

I want to add text to new line.

My code:

TextField text = new TextField();

TextArea area = new Area();

String txt = text.getValue().toString();

area.setValue("\n" + txt);

When I click in my button I see my value from TextField. I want new text in new line in TextArea. Please help.

You have to append that new value to the existing one and set that. Something along the lines of:

area.setValue(area.getValue() + "\n" + txt);

The Vaadin TextArea has no direct way to append. Also the rules in java, when to use + for stringsapply. Consider using a StringBuffer , if you do this alot.

Instead of

area.setValue("\n" + txt);

Use

area.setValue(area.getValue + "\n" + txt);

This will ADD text, not REPLACE

I recommend to add the new line at the end. You will avoid having an empty line at the beginning in your TextArea.

Eg outputTextArea.setValue(outputTextArea.getValue() + input.getValue() + "\\n");

Furthermore: Make sure to use a TextArea and not a TextField , because line breaks with "\\n" won't work in TextField !

Keep in mind that Vaadin works with browsers, so normal scape characters doesn't work, ie: you cannot use '\\n' on normal Label . Instead of it do the following:

label.setCaptionAsHtml(true);
label.setValue(label.getValue()+"<br>");

If you are using a TextArea , you can use '\\n' as usual. In case of TextFiel , you cannot write multiple lines, as it would convert it in a TextArea and this behavior is deprecated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM