Can we create pop-up error messages in java?

Is it possible to handle an exception by creating a pop-up warning?

+8
java
source share
3 answers

Of course, JOptionPane.

When you are dealing with Exception, you can use this:

try { } catch (Exception e) { JOptionPane.showMessageDialog(null, e.toString(), "Error", JOptionPane.ERROR_MESSAGE); } 

Or, when you just spotted your own error:

 if (someList.size() == 0) { JOptionPane.showMessageDialog(null, "List contained 0 elements!", "Error", JOptionPane.ERROR_MESSAGE); } 
+17
source share

Of course; use JOptionPane :

 JOptionPane.showMessageDialog(null, "ohnoes!", "ohnoes!", JOptionPane.ERROR_MESSAGE); 

Combine with UncaughtExceptionHandler as shown here and you are all set up.

+3
source share

You can create a class that implements Thread.UncaughtExceptionHandler and register it with Thread.setDefaultUncaughtExceptionHandler(...) .

In the public void uncaughtException(final Thread pThread, final Throwable pException) you can open an error dialog using JOptionPane.showMessageDialog(...) or something similar.

This will allow you to open a popup for each uncaught exception if you have a runtime that supports local gui, i.e. You are not working in headless mode or calling remote code.

+3
source share

All Articles