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;
/*// www . j ava2s .c o m
 Copyright (c) 1998-2013 The Regents of the University of California
 All rights reserved.
 Permission is hereby granted, without written agreement and without
 license or royalty fees, to use, copy, modify, and distribute this
 software and its documentation for any purpose, provided that the above
 copyright notice and the following two paragraphs appear in all copies
 of this software.

 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 PROVIDED HEREUNDER IS ON AN  BASIS, AND THE UNIVERSITY OF
 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 ENHANCEMENTS, OR MODIFICATIONS.

 PT_COPYRIGHT_VERSION_2
 COPYRIGHTENDKEY
 */

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.
     */
    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, ellipses
     * 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. inform(String dlgTitle, String info, Component parent)
  2. inform(String message)
  3. information(String title, String message)
  4. mensajeInformacion(String mensaje, String titulo)
  5. showException(Component parent, Exception e, String info)
  6. showInfo(Component owner, String msg)
  7. showInfoBox(Component aParent, String aTitle, String aMessage)
  8. showInfoMessage(Component parent, String message)
  9. showInfoMessage(String message, Component component)