1 package com.calipso.reportgenerator.common; 2 import javax.swing.*; 3 import java.awt.event.WindowEvent ; 4 import java.awt.event.WindowAdapter ; 5 import java.awt.event.ActionListener ; 6 import java.awt.event.ActionEvent ; 7 import java.awt.*; 8 import java.io.StringWriter ; 9 import java.io.PrintWriter ; 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 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 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 () { 41 public void windowClosing(WindowEvent evt) { 42 closeDialog(); 43 } 44 }); 45 aceptarBtn.addActionListener(new ActionListener () { 46 public void actionPerformed(ActionEvent 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 screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 66 setSize(new java.awt.Dimension (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 81 private String getStackTraceAsString(Exception e) { 82 StringWriter stringWriter = new StringWriter (); 83 PrintWriter printWriter = new PrintWriter (stringWriter); 84 e.printStackTrace(printWriter); 85 StringBuffer error = stringWriter.getBuffer(); 86 return error.toString(); 87 } 88 89 public static void main(String args[]) { 90 new ShowExceptionStackTrace(new JFrame(), true).setVisible(true); 91 } 92 } 93 | Popular Tags |