2

in our latest project in school, I got some problems. I want to observe a Path for new entries, the path is chosen by a file director button but if I choose any file, the whole window freezes... I guess it got frozen as the "observePath" method got called but I don't know how to fix this problem.

Here's the code:

public void start() {

    public Path absolutePath;
    final Label labelSelectedDirectory = new Label();
    Button btnOpenDirectoryChooser = new Button();
    btnOpenDirectoryChooser.setText("Open DirectoryChooser");

    btnOpenDirectoryChooser.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            DirectoryChooser directoryChooser = new DirectoryChooser();

            File selectedDirectory =
                    directoryChooser.showDialog(primaryStage);

            if(selectedDirectory == null) {
                labelSelectedDirectory.setText("No Directory selected");

            }else{
                labelSelectedDirectory.setText(selectedDirectory.getAbsolutePath());
                absolutePath = selectedDirectory.toPath();
                try {

                    observePath();

                } catch (IOException | InterruptedException e) {

                    e.printStackTrace();
                }
            }
        }
    });

public void observePath() throws IOException, InterruptedException {

        WatchService watcher = FileSystems.getDefault().newWatchService();
        FileSystem fs = FileSystems.getDefault();
        Path p = fs.getPath(absolutePath.toString());

        WatchKey key = p.register(watcher, StandardWatchEventKinds.ENTRY_CREATE);


            key = watcher.take();
            for (WatchEvent event : key.pollEvents()) {
                if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
                    System.out.println("found new data");
                }
                else {
                    System.out.println("no new data found");
                }
            }key.reset();
        }

    }

I hope someone can help me. Thank you very much

Tom

Omid Nikrah
  • 2,444
  • 3
  • 15
  • 30
TDI
  • 23
  • 1
  • 3
  • Welcome to StackOverflow, the first step is to add some debug code after every line, and you will quickly find out where it is freezing. Update your question to point to the specific part where it freezes and hopefully, we can point out a solution to what's happening. – sorifiend Sep 19 '18 at 08:40
  • 3
    `watcher.take();` is a blocking operation. This code must be executed in a separate thread. – mr mcwolf Sep 19 '18 at 08:41

1 Answers1

5

If your observePath method does heavy work you should execute it in a new Thread.

new Thread( ()->{
 observePath();
}).start();

Event handlers are executed in the JavafxApplicationThread which is responsible for updating the UI. You should not do any long lasting tasks in this thread or else you'll experience functionality loss.

More info in regards to the application thread can be found here How JavaFX application thread works?

Kilian
  • 1,540
  • 16
  • 28