Java Reference
In-Depth Information
text
=
new
new
JTextArea
(
40
,
40
);
cp
.
add
(
text
,
BorderLayout
.
CENTER
);
ok
=
new
new
JButton
(
"Close"
);
cp
.
add
(
ok
,
BorderLayout
.
SOUTH
);
ok
.
addActionListener
(
new
new
ActionListener
() {
public
public
void
void
actionPerformed
(
ActionEvent evt
) {
dispose
();
}
});
pack
();
}
/** Display the stackTrace from the given Throwable in this Dialog. */
void
void
showStackTrace
(
Throwable exc
) {
CharArrayWriter buff
=
new
new
CharArrayWriter
();
PrintWriter pw
=
new
new
PrintWriter
(
buff
);
exc
.
printStackTrace
(
pw
);
pw
.
close
();
text
.
setText
(
buff
.
toString
());
setVisible
(
true
true
);
}
}
See the program
ErrorUtilTest
in the
darwinsys/src/test/java
directory for an example of
running this program directly, and see
ErrorUtilCatchTest
in the same directory for an ex-
ample of using it with uncaught exceptions from the GUI thread as described.
Getting Program Output into a Window
Problem
You want to capture an input/output stream and display it in a text field.
Solution
The easy way is to subclass
OutputStream
or
Writer
as appropriate, and override just the
methods needed to get characters copied into the
JTextArea
. You may also want to redirect