Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamically updating JTextArea  RSS feed

 
kevin allen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im really stuck!

My program currently updates a JtextArea dynamically with the append method by
starting a new thread. The code is shown below...



I would like my program(the main thread) to wait in this procedure until the new thread(that updates the GUI) has finished. If i use the join() method
the jtextarea updates but not dynamically, it waits until the proc panelTextArea.setOutputText(text) has finished processing before updating.

Is there a way to have the program wait without using something like the following(its really slow)...




anyone know?

thanks
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Concurrency in Swing. In short, if your t.join() is executed in the Event Dispatching Thread (EDT) then that will block all other GUI related actions - mouse clicks, keyboard presses, repainting, etc. Any code that either can block or will take a long time should be executed in a different thread, possibly by using SwingWorker.
 
kevin allen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So there is no way to wait in the procedure while executing the EDT thread?

im confused because the loop solution i suggested does work albiet slowly, so is there no way of waiting here while still dealing with the EDT stuff?

a way of waiting without using t.join()?

 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kevin allen wrote:So there is no way to wait in the procedure while executing the EDT thread?

Kevin, you posted this 7 minutes after Rob Prime posted a link to a tutorial which very likely answers that question. You will not have been able to read and understand that whole tutorial in under 7 minutes. Read it. Please.
 
kevin allen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok fair enough, I solved the problem by changing the way the program is structured...

but to be fair to me this tutorial still does not answer the question of how to pause a thread in effectively the same way as my suggested loop without the overheads. However I appreciate the sort of help provided here so many thanks to people who spend there time helping persons such as myself kindness prevails.

thanks

kevin
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you don't want to stop the GUI from repainting, you will want to disable all your GUI input controls:
- disable all input controls
- start the new thread that will do the hard work
- when that thread has finished let it re-enable all controls again

In semi-pseudo code:
Usually the "update text field" is done using publish and process:
Note that JTextArea.append is special; it is already thread safe so the publish-process pair is not needed for it. For just about any other GUI updating code you should use the pair.


Note the use of Void in my pseudo code. Void is a special class that represents "nothing", and because it can never be instantiated at all it has only one valid value: null. I chose it because doInBackground doesn't need to return anything.
 
kevin allen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
again i extend gratitude towards endeavours to my answer my question, truly grateful and i hope you feel good as a result etc..

however this again technically doesnt answer my question..

what i asked for was a way of pausing the EDT thread whilst still doing EDT stuff providing the functionaility of a while(threadrunning) {} without
the massive performance issue..

kevin
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kevin allen wrote:again i extend gratitude towards endeavours to my answer my question, truly grateful and i hope you feel good as a result etc..
however this again technically doesnt answer my question..
what i asked for was a way of pausing the EDT thread whilst still doing EDT stuff providing the functionaility of a while(threadrunning) {} without
the massive performance issue..
kevin

And you've been given the answer: you don't want to pause the EDT. And I agree with this as I've yet to see anything yet that would suggest why you would need to do this -- nor would your users. A paused EDT means a frozen completely useless app. If another app goes over it, it will not repaint itself, if the user wants to move the scrollbars to see a portion of your JTextArea, the app won't respond. Your solution (as has already been given) is to disable your components and then from the SwingWorker, re-enable them when the process is done.

Here's a small example of disabling components vs. freezing the API. Run it and let me know which you think is a better solution:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!