KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > common > ShowExceptionStackTrace


1 package com.calipso.reportgenerator.common;
2 import javax.swing.*;
3 import java.awt.event.WindowEvent JavaDoc;
4 import java.awt.event.WindowAdapter JavaDoc;
5 import java.awt.event.ActionListener JavaDoc;
6 import java.awt.event.ActionEvent JavaDoc;
7 import java.awt.*;
8 import java.io.StringWriter JavaDoc;
9 import java.io.PrintWriter JavaDoc;
10
11
12
13 public class ShowExceptionStackTrace extends JDialog {
14   private JScrollPane scrollPane;
15   private JPanel conteinerBtnPanel;
16   private JPanel southPanel;
17   private JButton aceptarBtn;
18   private JTextArea textArea;
19
20  public static void showStackTrace(Exception JavaDoc e){
21    ShowExceptionStackTrace stackTrace = new ShowExceptionStackTrace(new JFrame(),true);
22    stackTrace.textArea.append(stackTrace.getStackTraceAsString(e));
23    stackTrace.setVisible(true);
24
25  }
26   public ShowExceptionStackTrace(java.awt.Frame JavaDoc parent, boolean modal) {
27     super(parent, modal);
28     initComponents();
29   }
30
31
32   private void initComponents() {
33     scrollPane = new JScrollPane();
34     textArea = new JTextArea();
35     southPanel = new JPanel();
36     conteinerBtnPanel = new JPanel();
37     aceptarBtn = new JButton();
38     textArea.setEditable(false);
39     setTitle("Stack Trace");
40     addWindowListener(new WindowAdapter JavaDoc() {
41       public void windowClosing(WindowEvent JavaDoc evt) {
42         closeDialog();
43       }
44     });
45     aceptarBtn.addActionListener(new ActionListener JavaDoc() {
46       public void actionPerformed(ActionEvent JavaDoc e) {
47         closeDialog();
48       }
49     });
50
51     scrollPane.setViewportView(textArea);
52
53     getContentPane().add(scrollPane, BorderLayout.CENTER);
54
55     southPanel.setLayout(new BorderLayout());
56
57     aceptarBtn.setText("Aceptar");
58     conteinerBtnPanel.add(aceptarBtn);
59
60     southPanel.add(conteinerBtnPanel, BorderLayout.EAST);
61
62     getContentPane().add(southPanel, BorderLayout.SOUTH);
63
64     pack();
65     java.awt.Dimension JavaDoc screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
66     setSize(new java.awt.Dimension JavaDoc(600, 500));
67     setLocation((screenSize.width - 600) / 2, (screenSize.height - 500) / 2);
68   }
69
70
71   private void closeDialog() {
72     setVisible(false);
73     dispose();
74   }
75
76   /**
77    * Retorna String object con el cpntenido de stack trace
78    * @param e
79    * @return
80    */

81   private String JavaDoc getStackTraceAsString(Exception JavaDoc e) {
82     StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
83     PrintWriter JavaDoc printWriter = new PrintWriter JavaDoc(stringWriter);
84     e.printStackTrace(printWriter);
85     StringBuffer JavaDoc error = stringWriter.getBuffer();
86     return error.toString();
87   }
88
89   public static void main(String JavaDoc args[]) {
90     new ShowExceptionStackTrace(new JFrame(), true).setVisible(true);
91   }
92 }
93
Popular Tags