user1147944
user1147944

Reputation: 23

Java - HTML tags for JLabel

I'm trying to get a JLabel to display new line characters by using HTML tags. But the text I want is obtained from a method. Here's the line of code:

myLabel.setText("<html><pre>myCart.toString()</pre></html>");

But, this literally sets the text of the label to myCart.toString(), not to the String that is returned by the method. Is there a way around this?

Upvotes: 2

Views: 1455

Answers (3)

jojo.math
jojo.math

Reputation: 39

"

myCart.toString()
"

should be

"

" + myCart.toString()+ "
"

But I am only repeating what Eng.Fouad said

Upvotes: 0

Sergej Raishin
Sergej Raishin

Reputation: 535

   myLabel.setText(MessageFormat.format("<html><pre>{0}</pre></html>",myCart.toString()));

Upvotes: 3

Eng.Fouad
Eng.Fouad

Reputation: 117645

Do you mean?

myLabel.setText("<html><pre>" + myCart.toString() + "</pre></html>");

or just:

myLabel.setText("<html><pre>" + myCart + "</pre></html>");

since toString() will be called implicitly.

Upvotes: 7

Related Questions