Java JOptionPane Error showErrorMessage(String title, String message, java.awt.Component parent)

Here you can find the source of showErrorMessage(String title, String message, java.awt.Component parent)

Description

Displays an error message using the JOptionPane#ERROR_MESSAGE variant of dialog.

License

Open Source License

Parameter

Parameter Description
title The title of the dialog.
message The error message.
parent The owner of this dialog.

Declaration

public static void showErrorMessage(String title, String message, java.awt.Component parent) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import javax.swing.JOptionPane;

public class Main {
    /**// www.ja v a2 s.  co  m
     * Displays an error message using the {@link JOptionPane#ERROR_MESSAGE}
     * variant of dialog.
     * <p>
     * <b>Note:</b> The error message will be split into 15-word long lines.</p>
     *
     * @param title The title of the dialog.
     * @param message The error message.
     * @param parent The owner of this dialog.
     */
    public static void showErrorMessage(String title, String message, java.awt.Component parent) {
        JOptionPane.showMessageDialog(parent, splitIntoLines(message), title, JOptionPane.ERROR_MESSAGE);
    }

    /**
     * Splits a given string into lines comprised of 15 words each. Line breaks
     * added to {@code message} by the user will be honoured and treated as
     * paragraphs.
     *
     * @param message a string.
     * @return a {@code String} split into lines.
     */
    public static String splitIntoLines(String message) {
        String[] messageSplitLineBreak = message.split("\n");
        StringBuilder mainBuilder = new StringBuilder();
        for (int i = 0; i < messageSplitLineBreak.length; i++) {
            String[] whiteSpaceSplit = messageSplitLineBreak[i].split(" ");
            StringBuilder tempBuilder = new StringBuilder();
            for (int j = 0; j < whiteSpaceSplit.length; j++) {
                tempBuilder.append(whiteSpaceSplit[j]).append(" ");
                if (j != 0 && j % 14 == 0 && message.length() > tempBuilder.length() + mainBuilder.length()) {
                    tempBuilder.append("\n");
                }
            }
            mainBuilder.append(tempBuilder).append("\n");
        }
        return mainBuilder.toString();
    }
}

Related

  1. showErrorMessage(JFrame frame, String msg)
  2. showErrorMessage(String message)
  3. showErrorMessage(String message, Component component)
  4. showErrorMessage(String message, int errorCode)
  5. showErrorMessage(String msg)
  6. showErrorMsg(final Container parent, final String message)
  7. showErrorPane(String errorMessage, Exception e)
  8. showErrorPanel(Component parent, String aS, Icon icon)
  9. showNoMacFoundError()