views:

69

answers:

2

I have a GUI which has textarea and buttons and etc.........

I have a different class which is a thread that is running constantly. I want to append text to the textarea when certain conditions are met in the thread class.

In the class containing the textarea i have getter methods for the textarea. I have also declared that class(containing the textarea) as a variable in the thread class.

I try to call the get method of the textarea on the variable and afterwards the append() method, but the textarea doesn't update itself.

what am i doing wrong?

thanks for your help.

+5  A: 

The main point is that you have to care about following guidelines given by Sun according to thread management with Swing, you can have a look here.

What you will understand is that you have to dispatch GUI related events with SwingUtilities.invokeLater(..) and SwingUtilities.invokeAndWait(..) according to your specific case. This because GUI events are managed by the Event Dispatching Thread and you shouldn't manage them in your own threads..

Jack
Some of the Swing documentation (less in JDK 7...) states that certain methods are thread-safe. Such comments often do not agree with the implementation, or any possible implementation. Also code attempting to use such methods in a threaded way will likely have thread bugs of its own.
Tom Hawtin - tackline
+1  A: 

You must not call UI methods from a non-UI thread. To call methods from a non-UI thread, you must a UI specific method which says "send a piece of code to the UI thread for execution". In SWT, this is Display.getDefault().asyncExec(Runnable). For Swing, use SwingUtilities.invokeLater().

See this article for more information.

Aaron Digulla