Java JOptionPane Error showError(Throwable e)

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

Description

show Error

License

BSD License

Declaration

public static void showError(Throwable e) 

Method Source Code


//package com.java2s;
// LICENSE:      This file is distributed under the BSD license.

import java.awt.event.ActionEvent;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Main {
    private static boolean show_ = true;

    public static void showError(Throwable e, String msg) {
        logError(e, msg);/*from   ww  w  .  j av  a  2 s  .c  om*/

        if (!show_)
            return;

        String fullMsg;
        if (e != null && e.getMessage() != null && msg.length() > 0) {
            fullMsg = "Error: " + msg + "\n" + e.getMessage();
        } else if (e != null && e.getMessage() != null) {
            fullMsg = e.getMessage();
        } else if (msg.length() > 0) {
            fullMsg = "Error: " + msg;
        } else if (e != null) {
            fullMsg = "Error: " + e.getStackTrace()[0];
        } else {
            fullMsg = "Unknown error (please check CoreLog.txt file for more information)";
        }

        int maxNrLines = 30;
        String test[] = fullMsg.split("\n");
        if (test.length < maxNrLines) {
            JOptionPane.showMessageDialog(null, fullMsg, "Micro-Manager Error", JOptionPane.ERROR_MESSAGE);
        } else {
            JTextArea area = new JTextArea(fullMsg);
            area.setRows(maxNrLines);
            area.setColumns(50);
            area.setLineWrap(true);
            JScrollPane pane = new JScrollPane(area);
            JOptionPane.showMessageDialog(null, pane, "Micro-Manager Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    public static void showError(Throwable e) {
        showError(e, "");
    }

    public static void showError(String msg) {
        showError(null, msg);
    }

    public static void showError(ActionEvent e) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    public static void logError(Throwable e, String msg) {
        if (e != null) {
            String stackTrace = getStackTraceAsString(e);
            logMessage(msg + "\n" + e.toString() + " in " + Thread.currentThread().toString() + "\n" + stackTrace
                    + "\n");
        } else {
            logMessage("Error: " + msg);
        }
    }

    public static void logError(Throwable e) {
        logError(e, "");
    }

    public static void logError(String msg) {
        logError(null, msg);
    }

    private static String getStackTraceAsString(Throwable aThrowable) {
        String result = "";
        for (StackTraceElement line : aThrowable.getStackTrace()) {
            result += "  at " + line.toString() + "\n";
        }
        Throwable cause = aThrowable.getCause();
        if (cause != null) {
            return result + "Caused by: " + cause.toString() + "\n" + getStackTraceAsString(cause);
        } else {
            return result;
        }
    }

    public static void logMessage(String msg) {
        System.out.println(msg);
    }
}

Related

  1. showError(String errorMsg)
  2. showError(String macAddress, String message)
  3. showError(String message)
  4. showError(String message)
  5. showError(String msg, String title)
  6. showError(Window parent)
  7. showErrorBox(Component aParent, String aTitle, String aMessage)
  8. showErrorBox(Component pParent, String pMessage, String pTitle)
  9. showErrorConfirmBox(Component pParent, String pMessage, String pTitle, String pLeftOption, String pRightOption)