import javax.swing.*; import au.com.forward.utils.StringUtils; import java.awt.EventQueue; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; /** * the main class of your application */ public class ExampleShowErrorMessage { private static String errMsgTitle = "Application Error"; // set to main frame of the application // used to attach error message dialog boxes to private static JFrame frame = null; // set to null so can still show errors if get error initializing JFrame //..... // other application method here //...... /** * Show an Error message * * @param window a class extending from JInternalFrame * that implements the displayError(Throwable t) method * Should be defined as an interface IWindow. * @param t the Throwable to be displayed */ public static void showErrorMessage(IWindow window, Throwable t) { // need to catch all errors here try { // do any special processing here // for example // if (t instanceof FileTransferStopped) { // ignore these as they are generated by other errors // return; // } if (window != null) { window.displayError(t); // display in the related window } else { Application.showErrorMessage(t); // no related window just show here } } catch (Throwable tex) { // this should never happen but lets be sure System.err.println("Caught exception in showErrorMessaage()\n"+StringUtils.toString(tex)); } } /** * Show an Error message * * @param t the Throwable Error to be shown */ public static void showErrorMessage(Throwable t) { // need to catch all errors here try { // do any special processing here // for example // if (t instanceof FileTransferStopped) { // ignore these as they are generated by other errors // return; // } // log the error here if appropriate // See "How to set up Java logging" for a better way System.err.println(StringUtils.toString(t)); JButton dismiss = new JButton("Continue"); JButton help = new JButton("Help"); JOptionPane messageBox = new JOptionPane("",JOptionPane.INFORMATION_MESSAGE); String msg = formatThrowableMessage(t); messageBox.setMessage(msg); Object[] options = {dismiss,help}; messageBox.setOptions(options); final JDialog dialog = messageBox.createDialog(frame,errMsgTitle); // if frame is null the messageBox is a top level component. final String helpID = getHelpContext(t); // add action listener for continue button dismiss.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { dialog.dispose(); } catch (Throwable tex) { // something bad happened System.err.println("Caught exception in when trying to close showErrorMessaage()\n" +StringUtils.toString(tex)); } } }); // add action listener for help button help.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { SwingUtilities.invokeLater(new Runnable() { public void run() { try { // this class (not shown here) displays this helpID using Java Help. // see "A suggestion for unified error messages" for more details ApplicationHelp.showHelp(helpID); } catch (Throwable tex) { // something bad happened System.err.println("Caught exception in when trying to show help()\n" +StringUtils.toString(tex)); } } }); } }); // this lets us call this method from any thread if (EventQueue.isDispatchThread()) { dialog.show(); } else { SwingUtilities.invokeLater(new Runnable() { public void run() { // need to catch all error here try { dialog.show(); } catch (Throwable tex) { // something bad happened System.err.println("Caught exception in when trying to show showErrorMessaage()\n" +StringUtils.toString(tex)); } } }); } } catch (Throwable tex) { // this should never happen but lets be sure System.err.println("Caught exception in showErrorMessaage()\n"+StringUtils.toString(tex)); } } /** * returns the error message to be displayed for this exception * * could look up international property files etc * for now just show the message */ private static String formatThrowableMessage(Throwable t) { return t.getMessage(); } /** * returns the helpID associated with this exception * * will use the class name as the helpID */ private static String getHelpContext(Throwable t) { return t.getClass().getName(); } public static void main(String args[]) { Throwable t = new OutOfMemoryError("The Application ran out of Memory.\nWill try to recover."); showErrorMessage(t); // need to kill this sample application manually // as Swing thread still running } }