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

Swing Timers - Where am I going wrong?  RSS feed

 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

The background to the task is that I have a class (FileHandler) that collects a group of files from the filesystem, compresses and encrypts them before connecting to a server and transmitting the files. This class is a Runnable class and I'm calling it like a Thread. It's doing a lot of work so it takes a while.

Now the task is that I need to show the status of this class in a JTextArea as it is happening. There can be big pauses when the Files are compressed so I need to tell the users what is happening. The FileHandler class passes Strings of information that should be displayed in this area.

This is how I'm doing it at the moment


When the printout's happen, I see the following
message1
message2
message3
message1THREAD
message2THREAD
message3THREAD

And what I determine from this is that my main process completes before this thread in updateTextArea fires.

I must be going about this the wrong way. Could someone please help me out?

Many thanks,
Rachel
 
Petr Blahos
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rachel,

Are you sure that the UI isn't busy with something else
at the same moment? invokeLater does the work when the
UI finished doing anything else.

I am implementing a progress bar in more or less the
same way. Sometimes it runs smoothly but sometimes it just
jumps quite a lot. That might be because the 'worker thread'
is quite greedy on resources so java just does not bother
to do the swing stuff.

Hope it helps,
Petr
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply

No, the UI is not doing anything else, it just sits there while all the other processing is taking place. I think that maybe the processing is, like you said, being "greedy" and eating all the resources so that java doesn't bother with the swing. How do I get it to be less greedy?

Rachel
 
Petr Blahos
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An idea: If you put something like Thread.sleep(1000); on some
places in your worker thread, (just for the debugging purposes)
that should definitely cause the context switching and the swing
thread should get to work. Not very helpful, but at least you will
see. If the messages are still broken that the problem is
elsewhere.

Best regards,
Petr
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I'll give that a bash and let you know how it does.

Thanks
Rachel
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That doesn't work. But I remember it used to work before I put a whole bunch of new stuff in. Thank goodess for backups. So now I'm going through the work for every day since I started this to find out where I've gone off the rails.

Thanks again for all the help. It's great to be part of this forum!

Cheers,
Rachel
[ August 26, 2004: Message edited by: Rachel Swailes ]
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This kind of code has worked well for me. I use a vector as a queue between the worker thread and the swing thread (Vector is thread safe!). It all seems a bit slow to start up, but on my PC (2.4 G Athlon) it takes about thee seconds to run once it starts.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there.

It looked like a good suggestion but I've implemented it in my code and there is no change to how it runs. The text area still gets filled only once the main executing thread is all finished.

Here's the new code



So still no solution as yet...

Cheers,
Rachel
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After what seems like ages the problem is finally solved!!! Thank you so much Eddie for that great solution. I've spent the weekend taking it to pieces and putting it back together and I took the plunge putting it back into my application and bingo! Hooray! Solved!

The problem with my first implementation of your example was that the vector was getting passed by value and not by reference for some silly reason (probably overlooked code on my part!) but now it's all good!

Thanks again!
Rachel
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rachel,

I liked what I saw of your solution as you have made it more OOP by not exposing the vector as I did. Great job. We learn from each other!

Ed
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!