• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setText not updating promptly

 
Ahmad Ras
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Concurrency in Swing. I knew this was the problem just by reading the subject.
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Foxtrot works in a similar way and may work for older Java versions as well:
http://foxtrot.sourceforge.net/
 
Ahmad Ras
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example:

 
Ahmad Ras
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys ....you are the best ! ))
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic