2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright (C) 2012, 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 2 of the GNU General Public License
7 * 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 {
22 public ExceptionDialog(final Exception exception) {
23 showException(exception);
30 public void showException(final Exception exception) {
32 final JFrame frame = new JFrame("Exception occured!");
33 final JPanel contentPanel = new JPanel(new BorderLayout());
34 final Throwable cause = exception.getCause();
38 final JPanel topPanel = new JPanel();
39 topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
42 final JLabel exceptionType = new JLabel("Exception type: "
43 + exception.getClass().getCanonicalName());
44 topPanel.add(exceptionType);
47 final JLabel message = new JLabel("Error message: "
48 + exception.getMessage());
49 topPanel.add(message);
53 if (cause.getMessage() != null) {
54 final JLabel message2 = new JLabel("Cause: "
55 + cause.getMessage());
56 topPanel.add(message2);
59 contentPanel.add(topPanel, BorderLayout.NORTH);
62 // build stack trace view
64 final StringBuffer buffer = new StringBuffer();
66 // if cause is available, show original stack trace
68 buffer.append("Stack trace:\n");
69 final StackTraceElement[] stackTrace = cause.getStackTrace();
70 for (final StackTraceElement stackTraceElement : stackTrace)
71 buffer.append(stackTraceElement.toString() + "\n");
73 // otherwise show at least current stack trace
74 buffer.append("Stack trace from original cause is not available.\nShowing current stack trace instead:\n");
76 for (final StackTraceElement stackTraceElement : new Exception(
77 "Stack trace").getStackTrace())
78 buffer.append(stackTraceElement.toString() + "\n");
81 final TextArea textArea = new TextArea(buffer.toString());
82 contentPanel.add(textArea, BorderLayout.CENTER);
86 frame.getContentPane().add(contentPanel);
87 frame.setSize(800, 600);
88 frame.setVisible(true);
90 // Thread.dumpStack();
95 * This method is for testing
97 public static void main(final String[] args) {
99 final Throwable cause = new Throwable("details.....");
101 final Exception exception = new Exception("test", cause);
103 new ExceptionDialog(exception);