2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 3 of the GNU Lesser General Public License
7 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.commons.gui.dialog;
12 import java.awt.BorderLayout;
13 import java.awt.TextArea;
15 import javax.swing.BoxLayout;
16 import javax.swing.JFrame;
17 import javax.swing.JLabel;
18 import javax.swing.JPanel;
20 public class ExceptionDialog {
23 * This method is for testing
26 * commandline arguments
28 public static void main(final String[] args) {
30 final Throwable cause = new Throwable("details.....");
32 final Exception exception = new Exception("test", cause);
34 new ExceptionDialog(exception);
37 public ExceptionDialog(final Exception exception) {
38 showException(exception);
45 public void showException(final Exception exception) {
47 final JFrame frame = new JFrame("Exception occured!");
48 final JPanel contentPanel = new JPanel(new BorderLayout());
49 final Throwable cause = exception.getCause();
53 final JPanel topPanel = new JPanel();
54 topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
57 final JLabel exceptionType = new JLabel("Exception type: "
58 + exception.getClass().getCanonicalName());
59 topPanel.add(exceptionType);
62 final JLabel message = new JLabel("Error message: "
63 + exception.getMessage());
64 topPanel.add(message);
68 if (cause.getMessage() != null) {
69 final JLabel message2 = new JLabel("Cause: "
70 + cause.getMessage());
71 topPanel.add(message2);
74 contentPanel.add(topPanel, BorderLayout.NORTH);
77 // build stack trace view
79 final StringBuffer buffer = new StringBuffer();
81 // if cause is available, show original stack trace
83 buffer.append("Stack trace:\n");
84 final StackTraceElement[] stackTrace = cause.getStackTrace();
85 for (final StackTraceElement stackTraceElement : stackTrace)
86 buffer.append(stackTraceElement.toString() + "\n");
88 // otherwise show at least current stack trace
89 buffer.append("Stack trace from original cause is not available.\nShowing current stack trace instead:\n");
91 for (final StackTraceElement stackTraceElement : new Exception(
92 "Stack trace").getStackTrace())
93 buffer.append(stackTraceElement.toString() + "\n");
96 final TextArea textArea = new TextArea(buffer.toString());
97 contentPanel.add(textArea, BorderLayout.CENTER);
101 frame.getContentPane().add(contentPanel);
102 frame.setSize(800, 600);
103 frame.setVisible(true);
105 // Thread.dumpStack();