How to add multiple lines in Label JavaFX

I have a problem that I really don't know how to add multiple lines to a Label in JavaFX.

For instance:

 Label label = new Label();
 for(int i= 0; i<10; i++){
     label.setText(Integer.toString(i));
 }

So, when the loop ends, the label only shows the final value, which is 9.

So, any solutions that can display all the numbers 1-9 with break lines (such as '\ n') between them.

This problem arises when I want to show Bill of my project, which contains many dishes. Thank you for your help.

+4
source share
1 answer

You need to add and not set the text over and over And you will need a new line character '\ n'

, , label.text

:

StringBuilder msg = new StringBuilder():
Label label = new Label();
for (int i = 0; i < 10; i++) {
    msg.append(Integer.toString(i));
    msg.append(",\n");  //this is the new line you need
}
label.setText(msg.toString());
+4

All Articles