Java JOptionPane Info showException(Component parent, Exception e, String info)

Here you can find the source of showException(Component parent, Exception e, String info)

Description

Display an exception in a nice user-oriented way.

License

Open Source License

Declaration

public static void showException(Component parent, Exception e, String info) 

Method Source Code


//package com.java2s;
import java.awt.Component;
import java.awt.Dimension;

import java.io.PrintWriter;
import java.io.StringWriter;

import javax.swing.JOptionPane;

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

public class Main {
    /** Display an exception in a nice user-oriented way.  Instead of 
     *  displaying the whole stack trace, just display the exception message
     *  and a button for displaying the whole stack trace.
     *//*  ww  w. j a v  a 2s .  c  om*/
    public static void showException(Component parent, Exception e, String info) {
        Object[] message = new Object[1];
        String string;
        if (info != null) {
            string = info + "\n" + e.getMessage();
        } else {
            string = e.getMessage();
        }
        message[0] = ellipsis(string, 400);

        Object[] options = { "Dismiss", "Display Stack Trace" };

        // Show the MODAL dialog
        int selected = JOptionPane.showOptionDialog(parent, message, "Exception Caught", JOptionPane.YES_NO_OPTION,
                JOptionPane.WARNING_MESSAGE, null, options, options[0]);

        if (selected == 1)
            showStackTrace(parent, e, info);

    }

    /** 
     * Return a string that contains the original string, limited to the
     * given number of characters.  If the string is truncated, elipses
     * will be appended to the end of the string
     */
    public static String ellipsis(String string, int length) {
        if (string.length() > length) {
            return string.substring(0, length - 3) + "...";
        }
        return string;
    }

    /** Display a stack trace dialog. Eventually, the dialog should
     * be able to email us a bug report.
     */
    public static void showStackTrace(Component parent, Exception e) {
        showStackTrace(parent, e, null);
    }

    /** Display a stack trace dialog. Eventually, the dialog should
     * be able to email us a bug report. The "info" argument is a
     * string printed at the top of the dialog instead of the Exception
     * message.
     */
    public static void showStackTrace(Component parent, Exception e, String info) {
        // Show the stack trace in a scrollable text area.
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        JTextArea text = new JTextArea(sw.toString(), 60, 80);
        JScrollPane stext = new JScrollPane(text);
        stext.setPreferredSize(new Dimension(400, 200));
        text.setCaretPosition(0);
        text.setEditable(false);

        // We want to stack the text area with another message
        Object[] message = new Object[2];
        String string;
        if (info != null) {
            string = info + "\n" + e.getMessage();
        } else {
            string = e.getMessage();
        }
        message[0] = ellipsis(string, 400);
        message[1] = stext;

        // Show the MODAL dialog
        JOptionPane.showMessageDialog(parent, message, "Exception Caught", JOptionPane.WARNING_MESSAGE);
    }
}

Related

  1. infoMsg(String msg)
  2. inform(String dlgTitle, String info, Component parent)
  3. inform(String message)
  4. information(String title, String message)
  5. mensajeInformacion(String mensaje, String titulo)
  6. showException(Component parent, Exception e, String info)
  7. showInfo(Component owner, String msg)
  8. showInfoBox(Component aParent, String aTitle, String aMessage)
  9. showInfoMessage(Component parent, String message)