This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Observe Folder with Swing Worker Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Observe Folder with Swing Worker Thread" Watch "Observe Folder with Swing Worker Thread" New topic
Author

Observe Folder with Swing Worker Thread

Florian Soko
Greenhorn

Joined: Sep 07, 2009
Posts: 3
Hi guys.

I'm new to Swing and currently I'm having some problems. I want to observe a certain folder, and whenever a new image file is copied to it or created in the folder, i want to display it in my Swing Application. So far I've made a simple app with a panel and an ImageIcon which I've read from the file system. This is working fine.

Now I want to expand my application. I thought I could make a swing worker thread (which is running all the time to observe the folder for new files).
So my plan was to get the file list of the directory, then sleep the worker thread for a while and then read the files again. If the length of the file list is longer than before, then i can return the last fileitem.

However I'm running into some trouble. It looks like the Worker Thread has no sleep function. And also my plan to observe the folder results in either an endless loop or my GUI is frozen until the Thread is finished. Because the plan was to never let the Thread finish, my GUI will be frozen forever.


I'd appreciate if anyone has some better ideads to observe a folder or could help me with these Worker Threads.

Greetings Florian
Alan Mehio
Ranch Hand

Joined: Apr 04, 2005
Posts: 73
Florian Soko wrote:Hi guys.

I'm new to Swing and currently I'm having some problems. I want to observe a certain folder, and whenever a new image file is copied to it or created in the folder, i want to display it in my Swing Application. So far I've made a simple app with a panel and an ImageIcon which I've read from the file system. This is working fine.

Now I want to expand my application. I thought I could make a swing worker thread (which is running all the time to observe the folder for new files).
So my plan was to get the file list of the directory, then sleep the worker thread for a while and then read the files again. If the length of the file list is longer than before, then i can return the last fileitem.

However I'm running into some trouble. It looks like the Worker Thread has no sleep function. And also my plan to observe the folder results in either an endless loop or my GUI is frozen until the Thread is finished. Because the plan was to never let the Thread finish, my GUI will be frozen forever.


I'd appreciate if anyone has some better ideads to observe a folder or could help me with these Worker Threads.

Greetings Florian


Just a quick idea; you extends your SwingWorker and do your process in doInBackground method ( implement) then you publish the result from reading the file system inside the doInBackground ( list of files) ; and you sleep the thread;

then you implement your GUI update to display the files ( JPanel in your case or any other JComponent) in the progress method

ex:





This code has not been tested;

Hope this could give you a better idea and give you a start



Regards,
Alan Mehio
London, UK
Florian Soko
Greenhorn

Joined: Sep 07, 2009
Posts: 3
Yes this is working.


You gave me a pretty good idea how i should make use of the SwingWorker and my GUI is now loading while the task is running in the background.

Thanks for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Observe Folder with Swing Worker Thread