Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text wrap in JOptionPane?

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?

like image 210
laksys Avatar asked Dec 23 '12 13:12

laksys


1 Answers

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.

like image 123
Andrew Thompson Avatar answered Sep 29 '22 09:09

Andrew Thompson