Possible Duplicate:
How do I run JOptionPane on EDT?
At seemingly random times (i.e. if the program is run enough times) a JOptionPane
window I have programmed will display a blank window the close & ok buttons are there but none of the text I coded. This seems to occur in any program written with JOptionPane
eventually; is there some way to prevent this? FWIW This rarely (if ever) occurs twice in a row.
I wrote this just now and ran it 15 times in a row without any problems, yet. It's exactly the same coding as I always use when using JOptionPane
. Is there something missing?
import javax.swing.JOptionPane;
public class SimpleJOptionPane
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "Hello, World!");
}//end main
}//end class SimpleJOptionPane
Generally this problem does not occur. The JOptionPane
usually displays with the intended text. What could have changed without my knowledge when it doesn't work?
As suggested in a comment, I am looking into using EventQueue.isDispatchThread()
The code below, as you can see, uses another method to display the JOptionPane. Is this an adequate solution?
final String ERR_TITLE = "Error";
final String ERR_MSG = "An exception has occured; please start over.";
showError(ERR_MSG, ERR_TITLE);
public static void showError(final String MESSAGE, final String TITLE)
{
JOptionPane.showMessageDialog(null, MESSAGE, TITLE, JOptionPane.ERROR_MESSAGE);
}//end showError