I have two threads in an application. Thread A displays a form andallows the user to interact with it, thread B executes a loop whichcontinually retrieves data and updates the form object by executing asynchronized method in the form object. I first tried to code thread A to use the showDialog() method on the formobject, then started thread a and then thread b. However, thread astarts and the form then waits to be closed before the statementstarting thread b is executed. I then tried just using the show()method in thread a – this allows the start of thread b to execute butI lose my interaction with the form.
All AWT components need to be painted and have events delivered fromthe same thread. Therefore, you should not call showDialog() or theshow method from different threads. This will produce unpredictablebehavior such as what you have descrbied. To ensure your actions onAWT components occur synchronously within the main AWT thread, use thejavax.swing.SwingUtilities invokeAndWait() or invokeLater() methods.Each one of these methods requires a Runnable argument that will beexecuted within the AWT thread. invokeLater() will returnimmediately, with the action being invoked when the AWT thread getsaround to it. invokeAndWait() will block and only return after theAWT thread has executed the Runnable instance.