aspose file tools
The moose likes Features new in Java 7 and the fly likes How to Watch directory in Swing GUI? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Features new in Java 7
Reply Bookmark "How to Watch directory in Swing GUI?" Watch "How to Watch directory in Swing GUI?" New topic
Author

How to Watch directory in Swing GUI?

Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 359
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

Joined: May 03, 2008
Posts: 4163
    
    3

http://download.oracle.com/javase/tutorial/essential/io/notification.html


luck, db
There are no new questions, but there may be new answers.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12911
    
    3

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to Watch directory in Swing GUI?
 
Similar Threads
Insider's Guide to Mixing Swing and JavaFX
Java Swing and RSS Feed Reader
displaying elapsed and remaining time
Directory Watch feature and Socket Server
invokeLater - what's the skinny?