0

I have a JTree which shows content of specific directory. When a download some file with ftp to this directory i want to let my JTree know about it and refresh his contents. How can i refresh JTree after some file added or removed from directory which is root for JTree?

btnRefreshContents.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            tree.getModel().
        }
    });
itro
  • 7,006
  • 27
  • 78
  • 121

2 Answers2

3

If you're using Java 7, you can use the new WatchService (FileSystem.newWatchService()) to detect changes to the filesystem. Here's an article on it.

As to updating the JTree component, take a look at this Stack Overflow page.

Community
  • 1
  • 1
Greg Kopff
  • 15,945
  • 12
  • 55
  • 78
  • You are right, but i have no other chooses.I have to use Java 1.5. Is it good idea to reload a JTree with the same directory again after adding files to it? – itro Apr 30 '12 at 11:28
0

You could add directory listener to the underlying tree model. There are many solutions for that. As already stated above, WatchService in Java 7 can be useful. But since you are not yet at Java 7 you can try other approaches:

JNotify - java library that allows java application to listen to file system events. Here is a sample (make sure you add dll/so dependency to your path.)

VFS from Apache Commons. Commons VFS provides a single API for accessing various different file systems. DefaultFileMonitor is a good example.

Community
  • 1
  • 1
tenorsax
  • 21,123
  • 9
  • 60
  • 107