Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Watch directory in Swing GUI?

 
Siegfried Heintze
Ranch Hand
Posts: 405
  • 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

 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://download.oracle.com/javase/tutorial/essential/io/notification.html
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15288
39
Android IntelliJ IDE Java Scala Spring
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic