Search code examples
javahtmlswingjoptionpane

JOptionPane displaying some HTML tags literally


I'm getting some inconsistent HTML display in a Swing message dialog. The first example call below is fine, but the second one displays one of the break tags as literal text. What's going on here?

import javax.swing.*;

class Test {
    public static void main(String... args) {
        SwingUtilities.invokeLater(() -> {
            JOptionPane.showMessageDialog(null,
                "<html>line 1<br>line 2<br>line 3");
            JOptionPane.showMessageDialog(null,
                "<html>line 1<br>\nline 2<br>\nline 3");
        });
    }
}

Solution

  • Don't know what the problem is but it looks like a JLabel renders the HTML correctly:

    JLabel label = new JLabel("<html>line 1<br>\nline 2<br>\nline 3");
    JOptionPane.showMessageDialog(null, label);
    

    Which doesn't really make sense because I thought a JOptionPane would us a JLabel to render the text?