title: Use width and max-width to wrap text in JOptionPane
body: <p>I don't have good HTML/CSS knowledge, so excuse any dramatic lack of knowledge. I'm working on a GUI in which I am producing a message dialog when an error occurs. This error message may be short or very long. I'm using the following code adapted from <a href="https://stackoverflow.com/a/14011645/3246372">this solution</a>.</p>
  
  <pre><code>import javax.swing.JFrame;
  import javax.swing.JOptionPane;
  
  public class TestOptionDialog {
  public static void main(String[] args) {
      String msgLong = "This is a freaking long messageThis is a freaking long messageThis is a freaking long messageThis is a freaking long messageThis is a freaking long messageThis is a freaking long messageThis is a freaking long messageThis is a freaking long messageThis is a freaking long messageThis is a freaking long messageThis is a freaking long messageThis is a freaking long messageThis is a freaking long messageThis is a freaking long message";
      String msgShort = "This is a freaking short message";
  
      int screenWidth = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
  
      JOptionPane.showMessageDialog(new JFrame(),
              "&lt;html&gt;&lt;body&gt;&lt;p style='width:100%;max-width:" + 0.6 * screenWidth + "px'&gt;" + msgLong + "&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;",
              "Error", JOptionPane.ERROR_MESSAGE);
  
      JOptionPane.showMessageDialog(new JFrame(),
              "&lt;html&gt;&lt;body&gt;&lt;p style='width:100%;max-width:" + 0.6 * screenWidth + "px'&gt;" + msgShort + "&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;",
              "Error", JOptionPane.ERROR_MESSAGE);
      }
  }
  </code></pre>
  
  <p>My problem is that if I only set the width variable to a certain pixel number (e.g. 0.6*screenWidth), too long messages are successfully wrapped, however for short messages the dialog is much larger than the text.
  On the contrary, if I only set the max-width argument, the short message is displayed with perfect size however too long messages are not wrapped to 60% of the screensize as I wanted to.</p>
  
  <p>So I thought, I'll just use both arguments and set width to 100% (As I read somewhere), but that still doesn't wrap too long messages. How can I achieve both things?</p>
  
  <p><strong>EDIT:</strong> After using Marius answer, which works quite well, I'm running into the following problem: even though the calculated width of the text is higher than the threshold (say, 60% of screenWidth), the dialog does not break. Does anybody understand what's going on or am I missing something? See the following code snippet and the output:</p>
  
  <pre><code>import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JOptionPane;
  
  public class TestOptionDialog {
  public static void main(String[] args) {
      // 1512 px
      String message = "message message message message message message message message message message message message message message message message message message message message message message message message message message message ";
      // 1232 px
      String message1 = "message message message message message message message message message message message message message message message message message message message message message message ";
      // 1064 px
      String message2 = "message message message message message message message message message message message message message message message message message message message ";
  
      // Break line at:
      int threshold = 1152;
  
      JOptionPane.showMessageDialog(new JFrame(),getFormattedErrorMessage(message, threshold),
              "Error", JOptionPane.ERROR_MESSAGE);
  
      JOptionPane.showMessageDialog(new JFrame(),getFormattedErrorMessage(message1, threshold),
              "Error", JOptionPane.ERROR_MESSAGE);
  
      JOptionPane.showMessageDialog(new JFrame(),getFormattedErrorMessage(message2, threshold),
              "Error", JOptionPane.ERROR_MESSAGE);
  
      System.exit(0);
  }
  
  private static String getFormattedErrorMessage(String message, int maxDialogWidth) {
      String string;
  
      JLabel label = new JLabel(message);
      if (label.getPreferredSize().width &gt; maxDialogWidth) {
          string = "&lt;html&gt;&lt;body&gt;&lt;p style='width:" + maxDialogWidth + "px;'&gt;" + message + "&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;";
      } else {
          string = "&lt;html&gt;&lt;body&gt;&lt;p&gt;" + message + "&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;";
      }
      return string;
      }
  }
  </code></pre>
  
  <p>1064 px:
  <a href="https://i.stack.imgur.com/JIFBQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JIFBQ.png" alt="1064 px"></a>
  1232 px:
  <a href="https://i.stack.imgur.com/vjnzR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vjnzR.png" alt="1232 px"></a>
  1512 px:
  <a href="https://i.stack.imgur.com/6wYZn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6wYZn.png" alt="1512 px"></a></p>