Search code examples
javamessageboxtext-formatting

How do I make a specific word (variable) in a message box bold in Java?


I'm trying to make one word (variable) of a message box bold in my Java program. Here is my code:

int n = messageBox.showConfirmDialog(frame,
"The File "+ file +" already exists." +
"\n" + "Do you want to replace it?",
"File Already Exists!",
messageBox.YES_NO_OPTION);

I want to make the variable "file" appear in bold text in my message box. So far I have only been able to get the entire message box to appear in bold, or none of it at all. How do I do this?


Solution

  • Try wrapping your text in html tags. Many of the swing components support some basic HTML such as italic, bold and underlining. For example, you should change your code to read:

    int n = messageBox.showConfirmDialog(frame,
    "<html>The File <b>"+ file +"</b> already exists." +
    "\n" + "Do you want to replace it?</html>",
    "File Already Exists!",
    messageBox.YES_NO_OPTION);