Cannot Call invokeAndWait From The Event Dispatcher Thread

In Swing, it is a pretty common mistake that running Threads can result in exceptions based on the event dispatcher. In my case, it happened when I called invokeAndWait() method in the SwingUtilities class. There was supposed to be another set of code to run that gets called with the invokeLater() but somehow the process flow skipped this one and went to the invokeAndWait() call instead.

To avoid such exception, you can use the boolean method of the SwingUtilities class called isEventDispatchThread(). If it returns true, you can call your set of code right away rather than placing it inside an invokeAndWait() method. If it is false, then have your set of code run within the invokeAndWait() method instead. Check the code below.

Related Posts Plugin for WordPress, Blogger...


Leave a Reply

Your email address will not be published. Required fields are marked *