When I try to use HTML with `JOptionPane`, HTML tags are printed instead of HTML formatting

jadrijan picture jadrijan · Jan 30, 2012 · Viewed 11.1k times · Source

For some weird reason when I try to use HTML with JOptionPane, HTML tags are printed instead of HTML formatting.

String msg = "Please fix <HTML><BODY BGCOLOR=#FFCCCC>this</BODY></HTML>";
JLabel message = new JLabel(msg);
JOptionPane.showMessageDialog(MyApp.this, message, "Error!", JOptionPane.ERROR_MESSAGE);

and the output is:

Please fix <HTML><BODY BGCOLOR=#FFCCCC>this</BODY></HTML>

Answer

mre picture mre · Jan 30, 2012

The entire string needs to be enclosed within the HTML tags. For example:

button = new JButton("<html><b><u>T</u>wo</b><br>lines</html>");

For more information, see How to Use HTML in Swing Components.