user10724876
user10724876

Reputation:

Display part of JLabel in bold

People have asked similar questions to this but I don't think they quite help with my question.

I've got a GUI with a JLabel that displays the current value of a String. I would like to put part of a string in bold. The string itself can vary, so I'm not sure HTML will be of help?

String myStr1 = "abc";
String myStr2 = "this bit in bold";
String myStr3 = "now back to normal";
myLabel.setText(myStr1 + [BOLD] myStr2 + myStr3);

It seems like this should be simple, I assumed there'd be an escape character or something, but I can't find an easy way.

Upvotes: 5

Views: 5159

Answers (1)

geneSummons
geneSummons

Reputation: 925

Use HTML tags

myLabel.setText("<html>" + str1 + "<B>" + str2 + "</B>" + str3 + "</html>")

See: How to Use HTML in Swing Components.

Upvotes: 7

Related Questions