I am listening for changes in a folder/file but when there are changes I do not receive an event

Apache NetBeans Wiki Index

Note: These pages are being reviewed.

If you are adding/deleting/changes using the FileSystems API - i.e. adding using FileUtil.createData() then it should work.

If not, get the parent folder as a FileObject and call theFolder.getFileSystem().refresh().

NetBeans will automatically do the refresh if focus is shifted away from the main window and back. However if files are created by an external process, or by using java.io.File.mkdir() or java.io.File.createNewFile() (which you shouldn’t be using inside a NetBeans module unless you really need to), changes will not be noticed until something forces a refresh.

The reasons for the way this is handled are twofold:

  1. There is not native support in Java for listening for changes in a file/folder (yet), though most OS’s natively support it

  2. We used to use a system that polled on a timer, but this is a bad idea because the user can be using, say, email, and most of the in-memory copy of NetBeans can have been swapped out to disk by the OS - but to check for modified files, it has to be dragged back into memory out of the swap file to see if some files have changed, even though the user is using another application. That hurts performance of the user’s other applications.

If you are creating files or folders programmatically, use FileObject, not java.io.File and any nodes your UI shows for those files will automatically update.