Java Swing How to - Break a message in two or more lines in JOptionPane with HTML








Question

We would like to know how to break a message in two or more lines in JOptionPane with HTML.

Answer

import java.awt.BorderLayout;
/*from ww w . ja v a 2  s  . c o  m*/
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Main {

  public static void main(String[] args) {
    String pt1 = "<html><body width='";
    String pt2 = "'><h1>Label Width</h1>"
        + "<p>Many Swing components support HTML 3.2 &amp;"
        + " (simple) CSS.  This is a new line.<br><br>"
        + "<p>The body width in this text is set to " + "";
    String pt3 = " pixels." + "";

    JPanel p = new JPanel(new BorderLayout());

    int width = 175;
    String s = pt1 + width + pt2 + width + pt3;

    JOptionPane.showMessageDialog(null, s);
  }
}