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
25 public static void main(final String[] args) {
27 final Throwable cause = new Throwable("details.....");
29 final Exception exception = new Exception("test", cause);
31 new ExceptionDialog(exception);
34 public ExceptionDialog(final Exception exception) {
35 showException(exception);
42 public void showException(final Exception exception) {
44 final JFrame frame = new JFrame("Exception occured!");
45 final JPanel contentPanel = new JPanel(new BorderLayout());
46 final Throwable cause = exception.getCause();
50 final JPanel topPanel = new JPanel();
51 topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
54 final JLabel exceptionType = new JLabel("Exception type: "
55 + exception.getClass().getCanonicalName());
56 topPanel.add(exceptionType);
59 final JLabel message = new JLabel("Error message: "
60 + exception.getMessage());
61 topPanel.add(message);
65 if (cause.getMessage() != null) {
66 final JLabel message2 = new JLabel("Cause: "
67 + cause.getMessage());
68 topPanel.add(message2);
71 contentPanel.add(topPanel, BorderLayout.NORTH);
74 // build stack trace view
76 final StringBuffer buffer = new StringBuffer();
78 // if cause is available, show original stack trace
80 buffer.append("Stack trace:\n");
81 final StackTraceElement[] stackTrace = cause.getStackTrace();
82 for (final StackTraceElement stackTraceElement : stackTrace)
83 buffer.append(stackTraceElement.toString() + "\n");
85 // otherwise show at least current stack trace
86 buffer.append("Stack trace from original cause is not available.\nShowing current stack trace instead:\n");
88 for (final StackTraceElement stackTraceElement : new Exception(
89 "Stack trace").getStackTrace())
90 buffer.append(stackTraceElement.toString() + "\n");
93 final TextArea textArea = new TextArea(buffer.toString());
94 contentPanel.add(textArea, BorderLayout.CENTER);
98 frame.getContentPane().add(contentPanel);
99 frame.setSize(800, 600);
100 frame.setVisible(true);
102 // Thread.dumpStack();