简体   繁体   中英

Java GUI and threads

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.

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..

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM