Java Reference
In-Depth Information
You can use the
JOptionPane
class in several other ways. For example, you can call its
showDialog()
method with a list of strings; each is displayed on a push button in the dialog.
This method blocks until the user selects one of the buttons; the return value of the method is
an
int
telling which button the user clicked (it returns the array index of the string whose
button was pressed). Another method,
showInputDialog()
, lets you prompt the user for a
data value. Very, very convenient!
See Also
JDialog
lets you write arbitrarily complicated dialogs. You subclass them in a manner simil-
ar to
JFrame
, specifying whether you want an application-modal or nonmodal dialog (a mod-
al dialog locks out the rest of the application, which is less convenient for the user but much
Catching and Formatting GUI Exceptions
Problem
Your application code is throwing an exception, and you want to catch it, but the GUI runs in
Solution
Use
setDefaultUncaughtExceptionHandler()
.
Discussion
In days of yore, we had to use an unsupported feature for this! Now we use the
Thread
meth-
od
setDefaultUncaughtExceptionHandler()
, whose signature is:
public static void setDefaultUncaughtExceptionHandler(
Thread.UncaughtExceptionHandler handler);
The code in
Example 14-6
shows a tiny demonstration of this technique.
Example 14-6. ThreadBasedCatcher.java