4

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

For example:

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

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

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

This problem that happens when i want to show the Bill of my project that contain many dishes. Thank you for your help.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
pnmtu13894
  • 55
  • 1
  • 5
  • 2
    Possible duplicate of [Control for displaying multiline text?](http://stackoverflow.com/questions/15977295/control-for-displaying-multiline-text) – Arnaud Apr 12 '16 at 08:50
  • Thank you but in my situation, the values when i put into the label are not available yet. – pnmtu13894 Apr 12 '16 at 08:54

1 Answers1

3

You need to append and not set the text over and over again AND you need the new line character '\n'

my suggestion would be like using a variable to append the information and when you are done with that step, then set the label.text

Example:

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());
Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97