• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Observe Folder with Swing Worker Thread

 
Florian Soko
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Florian Soko
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic