Tuesday, July 22, 2014

Allow java swing to send all error messages to a JOptionPane

By default java swing will send error messages to the java console.  This would be ok if the java console would pop up from a runnable java application, but it does not and have not found a way to do so.  If otherwise instructed, java will hide all messages (even fatal ones) from the user during runtime.  This is not acceptable behavior, so I have found a solution.  Route the messages to JOptionPanes like a popup.

During the main(String[] args) procedure, insert the following code:

public static void main(String[] args) {
System.out.println(SwingUtilities.isEventDispatchThread());
SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.out.println(SwingUtilities.isEventDispatchThread());
try {
Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null,
   e.toString(),
   "Error",
   JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
});
                                        ****START CODE HERE****


System.out.println(SwingUtilities.isEventDispatchThread());

} catch (Exception e) {
e.printStackTrace();
}
}
});
}

No comments:

Post a Comment