DingDongDang132
DingDongDang132

Reputation: 67

How can I add text to a Label without overwriting?

I want to add text to a Label in JavaFX without removing the previous text. I've tries this but it just overwrites all of it:

Label label = new Label();
label.setText("Line1");
label.setText("Line2");

For instance, if I want it to say "Line1Line2". Is there no other way than the following:

Label label = new Label();
label.setText("Line1");
label.setText("Line1Line2");

Upvotes: 1

Views: 903

Answers (1)

Andrew
Andrew

Reputation: 49646

You can get the previous label, concatenate it with a new part and then set a whole text:

label.setText(label.getText() + "Line2");

There are no other ways since we have access only to a getter/setter; API doesn't provide a more convenient method (like a builder/appender).

Upvotes: 3

Related Questions