This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes setText not updating promptly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "setText not updating promptly" Watch "setText not updating promptly" New topic
Author

setText not updating promptly

Ahmad Ras
Greenhorn

Joined: Jun 09, 2010
Posts: 11
Hi all,
I am trying to use JTextPane with setText method to update the text area. However my text gets displayed all at once rather than one at a time.
Ie.

textField.setText("processing phase 1");
doSomeWork()
textField.setText("processing phase 2");
doMoreWork()
textField.setText("processing phase 3");


However I get the the 3 displayes all at once after all is donne!.. Any helpppppp?Thank you
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Concurrency in Swing. I knew this was the problem just by reading the subject.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
I suggest using SwingWorker (Rob already linked to the relevant tutorial) for the 'work'. Such topics seem to appear regularly:
http://www.coderanch.com/t/498875/GUI/java/JButton-Got-Hanged

Swing + Concurrency is part of the sun swing tutorials, but maybe we should also add it to the FAQ here at the ranch?

Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Peter Taucher wrote:Swing + Concurrency is part of the sun swing tutorials, but maybe we should also add it to the FAQ here at the ranch?

Good idea. You can edit the FAQ and it if you want to.


[My Blog]
All roads lead to JavaRanch
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
So I created a code example for the FAQ:
http://faq.javaranch.com/java/ComponentDoesntUpdatePromptly
and added it to the category SwingFaq:
http://faq.javaranch.com/java/SwingFaq
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

That looks a lot like JProgressBarDoesntUpdate
That doesn't mean I disapprove of this entry; it's a generalization of the JProgressBar example which seems to never be found because, well, most people have the problem with other components. It's just funny to see how much similarities there are in the text. Kind of like quite a bit of copy-paste work.
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
It's similar but not the same. In the ProgressBar example it's very specific whereas I tried to formulate the text for all possible components. And the second huge difference is the use of SwingWorker in the new example, whereas in the old one the threading is handled directly. I thought that maybe the ProgressBar example was founded on a Java API that didn't provide SwingWorker (it's pretty new, you know).
Ahmad Ras
Greenhorn

Joined: Jun 09, 2010
Posts: 11
Hi all
thank you for your help I really appreciate it
however from the reading of swing worker it seems it is for jre 1.6
is there a way I can get it to work with1.4? There is no flush or a method I can call that repaint the whole thing

thank you again. And good idea to update the FAQ
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
Foxtrot works in a similar way and may work for older Java versions as well:
http://foxtrot.sourceforge.net/
Ahmad Ras
Greenhorn

Joined: Jun 09, 2010
Posts: 11
Hi all,
First of all thank you for the feedback. Its indeed helpful however I still have a question I am not sure which process to thread. As an example, I am reading the file line by line and I want to display it in the JTextArea. HOWEVER I want them to be synchronized. Meaning, I read a line, display, read next line display ..etc.
Do I start reading the lines one by one and start a thread (or swing worker) for each display update or the other way around? How Do I make sure that I read the next line after the display is done?
THHHHHHANNNKK UUU
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Sounds like work for a SwingWorker in combination with its pair of publish / process. In short, you publish any number of lines (probably one at a time) in your doInBackground() method. Then at some the process method is called in the EDT; the List will contain all non-processed lines so far; it's can contain the lines collected by multiple calls to publish. It won't be completely real-time but it's going to be as close as you're going to get.
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
Example:

Ahmad Ras
Greenhorn

Joined: Jun 09, 2010
Posts: 11
Thanks guys ....you are the best ! ))
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setText not updating promptly
 
Similar Threads
JTextField value is refreshing
TextField
silly Doubt regarding transation and compilation
processing events
Any one Can help me with Focus system?