-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorDisplayDialog.java
More file actions
86 lines (68 loc) · 2.41 KB
/
ErrorDisplayDialog.java
File metadata and controls
86 lines (68 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.ggl.error.display.view;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* This {@code JDialog} displays a Java {@code Exception}. The format of the
* output is the same as the output provided by the {@code printStackTrace}
* method.
*
* @author Gilbert G. Le Blanc - 8 December 2021
*
*/
public class ErrorDisplayDialog extends JDialog {
private static final long serialVersionUID = 1L;
private JTextArea textArea;
/**
* This constructor creates and displays the error display dialog.
*
* @param frame - The parent component of the dialog.
* @param title - The title of the dialog.
* @param e - The {@code Exception being thrown}
*/
public ErrorDisplayDialog(JFrame frame, String title, Exception e) {
super(frame, title, true);
add(createMainPanel(e), BorderLayout.CENTER);
add(createButtonPanel(), BorderLayout.SOUTH);
pack();
setLocationRelativeTo(frame);
setVisible(true);
}
private JPanel createMainPanel(Exception e) {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
textArea = new JTextArea(24, 90);
textArea.setEditable(false);
textArea.setMargin(new Insets(5, 5, 5, 5));
textArea.setText(generateException(e));
textArea.setCaretPosition(0);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JButton button = new JButton("Cancel");
button.addActionListener(event -> ErrorDisplayDialog.this.dispose());
panel.add(button);
return panel;
}
private String generateException(Exception e) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
PrintStream stream = new PrintStream(bytes);
e.printStackTrace(stream);
stream.close();
return new String(bytes.toByteArray());
}
}