This week's book giveaways are in the Angular and TypeScript and Web Services forums.
We're giving away four copies each of Programming with Types and The Design of Web APIs and have the authors on-line!
See this thread and this one for details.
Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

How to Watch directory in Swing GUI?

 
Ranch Hand
Posts: 420
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this nice console mode example of watching a directory and it even works!
Watch Directory example

This code looks a little CPU intensive: we are in a nested for loop! Are we actually polling the file system continually? Yikes! In C++ on windows, you set up a call back so you can sleep instead of poll.

Can anyone show me a Swing GUI example that demonstrates this directory watch feature?

Now I did a little google searching with no luck. How would I write a Swing GUI version of this example? Do I have to execute the watchFile function in a child thread and then use the javax.swing.SwingWorker from java1.6? How would I join with such a thread when I am exiting the application?

Thanks!
Siegfried

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://download.oracle.com/javase/tutorial/essential/io/notification.html
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Siegfried Heintze wrote:This code looks a little CPU intensive: we are in a nested for loop! Are we actually polling the file system continually? Yikes! In C++ on windows, you set up a call back so you can sleep instead of poll.


No, it is not polling the file system and it is not CPU intensive. The watcher.take() call in line 21 of the code example is a blocking call: it makes the thread wait (on the OS level) until some interesting event happens.

The whole idea of the file system watcher API is that on operating systems that support it, it will use the OS'es native support. So under the covers it's using Windows' native support for watching changes in a directory.
 
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!