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

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

Description

Display a stack trace dialog.

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*// www  . j a  va  2 s .co  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 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);
    }

    /**
     * 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;
    }
}

Related

  1. showInformationConfirmBox(Component pParent, String pMessage, String pTitle, String pLeftOption, String pRightOption)
  2. showInformationMessage(Component component, String message, String title)
  3. showInformationMessage(Component parent, String str)
  4. showInformationMessage(Component parent, String str)
  5. showInformationMessage(final JComponent parent, final String messge, final String title)