Java tutorial
//package com.java2s; import java.awt.Component; import javax.swing.JDialog; import javax.swing.JOptionPane; public class Main { /** Show a warning message dialog box with message word wrapping. */ public static void warningMessageBox(Component parent, String message) { messageBox(parent, "Warning", JOptionPane.WARNING_MESSAGE, message); } /** Show a message dialog box with message word wrapping, specified * title and type. Type must be one of the JOptionPane *_MESSAGE * constants. * * @see JOptionPane.setMessageType */ public static void messageBox(Component parent, String title, int messageType, String message) { JOptionPane pane = makeWordWrapJOptionPane(); pane.setMessage(message); pane.setMessageType(messageType); JDialog dialog = pane.createDialog(parent, title); dialog.setVisible(true); } /** Create a JOptionPane instance that word-wraps its message. */ public static JOptionPane makeWordWrapJOptionPane() { // The basic problem is described in this bug report: // // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4104906 // // The workaround there requires adding a scrollbar to the // message, which I do not want to do. // I tried these solutions, but they do not work (anymore?): // // http://stackoverflow.com/questions/4330076/joptionpane-showmessagedialog-truncates-jtextarea-message // http://www.coderanch.com/t/339970/GUI/java/wrap-large-message-JOptionPane-showConfirmDialog // // Most other solutions involve manually inserting newlines. // Thankfully, this one actually does work: // // http://www.jroller.com/Fester/entry/joptionpane_with_word_wrapping @SuppressWarnings("serial") JOptionPane pane = new JOptionPane() { @Override public int getMaxCharactersPerLineCount() { return 80; } }; return pane; } }