31

I'm using following code to display error message in my swing application

try {
    ...
} catch (Exception exp) {
    JOptionPane.showMessageDialog(this, exp.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}

The width of the error dialog goes lengthy depending on the message. Is there any way to wrap the error message?

mKorbel
  • 108,320
  • 17
  • 126
  • 296
laksys
  • 2,978
  • 4
  • 23
  • 35

2 Answers2

57

A JOptionPane will use a JLabel to display text by default. A label will format HTML. Set the maximum width in CSS.

JOptionPane.showMessageDialog(
    this, 
    "<html><body><p style='width: 200px;'>"+exp.getMessage()+"</p></body></html>", 
    "Error", 
    JOptionPane.ERROR_MESSAGE);

More generally, see How to Use HTML in Swing Components, as well as this simple example of using HTML in JLabel.

Community
  • 1
  • 1
Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
  • 1
    @laksys: Exactly, the `String` is automatically wrapped in a `JLabel`, which supports HTMl. – trashgod Dec 23 '12 at 18:51
  • 5
    Don't forget to html-escape the string, otherwise some special characters might not work. – Yogu Jan 16 '14 at 22:01
  • 1
    Exactly. E.g. using `\n` makes the code to not work, has to use `
    ` instead.
    – Math Feb 17 '14 at 13:41
  • 2
    For the record, it is not necessary to close the tags, or even open the body. As long as there is the `` tag then Java parses the HTML just fine. – Opal Mar 16 '14 at 08:58
  • 2
    @Lee why would you advise anyone to not close tags? – pstanton Jun 05 '14 at 23:21
  • 2
    For the `` tag it is not necessary to close it(others maybe). It's just quicker, easier and probably more readable. – Opal Jun 06 '14 at 23:40
  • So, add `` and `` tag around the text and escape the text will suffice? Nice. But how can we make it an utility method, or any util jar just can do it well? This is a typical high frequency requirement. – WesternGun Aug 22 '17 at 11:56
  • @FaithReaper The **style** (setting the width) is a vital part of making it working, not just wrapping it in HTML. You can confirm that by changing the simple example above to remove the style. *"But how can we make it an utility method"* IT's be pretty trivial to make a method that accepts a `String` (the text to be displayed) as well as an `int` (preferred width) then add the HTML/CSS around the (escaped) text using the int value for the width in an option pane. For more versatility, have the method return the 'HTML friendly' string instead of displaying it, then the programmer can .. – Andrew Thompson Aug 22 '17 at 12:30
  • .. decide how and where to display it. If that doesn't answer your query, then I apparently don't understand what you mean (which is quite possible). – Andrew Thompson Aug 22 '17 at 12:31
  • @AndrewThompson yes, I think setting the width is necessary. As for the utility method, I mean not only add the style, but to escape certain characters to display in HTML. I doubt there is no wheel invented. – WesternGun Aug 23 '17 at 07:19
32

Add your message to a text component that can wrap, such as JEditorPane, then specify the editor pane as the message to your JOptionPane. See How to Use Editor Panes and Text Panes and How to Make Dialogs for examples.

Addendum: As an alternative to wrapping, consider a line-oriented-approach in a scroll pane, as shown below.

error image

f.add(new JButton(new AbstractAction("Oh noes!") {
    @Override
    public void actionPerformed(ActionEvent action) {
        try {
            throw new UnsupportedOperationException("Not supported yet.");
        } catch (Exception e) {
            StringBuilder sb = new StringBuilder("Error: ");
            sb.append(e.getMessage());
            sb.append("\n");
            for (StackTraceElement ste : e.getStackTrace()) {
                sb.append(ste.toString());
                sb.append("\n");
            }
            JTextArea jta = new JTextArea(sb.toString());
            JScrollPane jsp = new JScrollPane(jta){
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(480, 320);
                }
            };
            JOptionPane.showMessageDialog(
                null, jsp, "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
}));
trashgod
  • 196,350
  • 25
  • 213
  • 918