Friday, December 4, 2009
Writing your own events not eventlisteners in Java
So I've never had to do this before and it certainly isn't taught in any Java classes. They never even approach writing your own events in java. I think they barely scratch the surface of using listeners, but I wanted to write my own Events that could occur and not event listeners. Well it's not as bad as you think. These two resources explain it pretty well.
http://184.108.40.206/thread.jspa?threadID=5185967&messageID=9725297 - Java forums thread
http://www.exampledepot.com/egs/java.util/CustEvent.html - The first example depot example I came across.
The java forums thread actually tells you how to receive events when a directory or file changes and a little modification made it easy to actually get the directory that was changed.
Essentially the trick is to create three classes, a class that extends the EventObject class, an interface that extends the EventListener class, and your own class that actually performs whatever you want to watch events for. The screenshot above shows a watched directory changed using the code based on the links above. The returned directory here has had a New Folder folder added and hence the aerosmith directory has changed.
Basically one step closer to automated folder watching.