Java JOptionPane Error displayError(Throwable e)

Here you can find the source of displayError(Throwable e)

Description

display Error

License

Apache License

Declaration

public static void displayError(Throwable e) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.EventQueue;

import java.awt.KeyboardFocusManager;

import java.awt.Window;

import javax.swing.JOptionPane;

public class Main {
    public static void displayError(Throwable e) {
        displayMessageBox(e == null ? "Error performing operation." : e.getMessage(), JOptionPane.ERROR_MESSAGE,
                false);//from  w  w w .j a  va 2  s . co m
    }

    public static void displayError(String strMsg) {
        displayMessageBox(strMsg, JOptionPane.ERROR_MESSAGE, false);
    }

    public static void displayMessageBox(String strMsg, final int iType, boolean bWrapText) {
        final String strWrappedMsg = bWrapText ? wrapText(strMsg) : strMsg;

        Runnable logMsgBox = new Runnable() {
            public void run() {
                JOptionPane.showMessageDialog(getWindow(), strWrappedMsg, "", iType);
            }
        };

        if (EventQueue.isDispatchThread()) {
            logMsgBox.run();
        } else {
            try {
                EventQueue.invokeAndWait(logMsgBox);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }

    public static String wrapText(String strText) {
        return wrapText(strText, 60);
    }

    public static String wrapText(String strText, int iLineLen) {
        StringBuilder sb = new StringBuilder();
        while (strText != null) {
            if (strText.length() > iLineLen) {
                sb.append(strText.substring(0, 60)).append("\n");
                strText = strText.substring(60);
            } else {
                sb.append(strText);
                strText = null;
            }
        }
        return sb.toString();
    }

    public static Window getWindow() {
        return KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
    }

    public static Window getFocusedWindow() {
        return KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
    }
}

Related

  1. askForAStringInArrayReturning(String question, String header, T[] options, String errorMessage, String errorHeader)
  2. contChoose(final Component parent, final String title, final String message, final String noContMsg, boolean cont, boolean error)
  3. createAndShowErrorPanel(String title, Map messages)
  4. displayError(Component parent, String title, String message)
  5. displayError(String message)
  6. displayErrorMessage(Component component, Throwable t)
  7. displayErrorMessage(Component parent, String message)
  8. displayErrorMessage(Component parentComponent, String message, String windowTitle)
  9. displayErrorMessage(final Throwable throwable, final Component parentComponent)