Display Multiline Messages with JOptionPane in Java
Description
The following code shows how to display Multiline Messages with JOptionPane.
Example
/*from w w w.jav a 2s. co m*/
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] a) {
String msg = "<html>this is a really long message<br>this is a really long message this is a really long message this is a really long message this is a really long message this is a really long message this is a really long message";
JOptionPane optionPane = new NarrowOptionPane();
optionPane.setMessage(msg);
optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = optionPane.createDialog(null, "Width 100");
dialog.setVisible(true);
}
}
class NarrowOptionPane extends JOptionPane {
NarrowOptionPane() {
}
public int getMaxCharactersPerLineCount() {
return 100;
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »