javafx 绑定按钮禁用 属性 到文件夹存在

javafx bind button disable property to folder exists

我有一个带有“继续”按钮的阶段,在创建文件夹“folderFramework”之前要将其禁用。

这是我的代码片段,不幸的是它不起作用。

File folderFramework = new File("path to the folder");
    ObjectProperty<File> file = new SimpleObjectProperty<>();
    file.set(folderFramework); // tested with setValue too...

    BooleanBinding fileExists = Bindings.createBooleanBinding(() ->
                    file.get() != null && file.get().exists(),
            file);

    btnContinue.disableProperty().bind(fileExists.not());

我错过了什么?谢谢!

任务中的 WatchService 帮了我大忙。这是最终的解决方案:

private void handleBtnContinue() {

    // task definition for watching changes in framework folder
    Task<Void> watchingTask = new Task<>() {

        @Override
        protected Void call() {

            WatchService watchService;
            try {
                watchService = FileSystems.getDefault().newWatchService();


                Path path = Paths.get(ConstantUtils.getPathFolderFramework());

                path.register(
                        watchService,
                        StandardWatchEventKinds.ENTRY_CREATE,
                        StandardWatchEventKinds.ENTRY_DELETE,
                        StandardWatchEventKinds.ENTRY_MODIFY);

                WatchKey key;
                boolean quitTask = false;
                while ((key = watchService.take()) != null && !quitTask) {
                    for (WatchEvent<?> event : key.pollEvents()) {

                        // when src folder is cloned -> enable the button continue
                        if (event.kind().toString().equals("ENTRY_MODIFY") && event.context().toString().equals("src")) {

                            // enable btn continue
                            Platform.runLater(() -> btnContinue.setDisable(false));

                            // end the task
                            quitTask = true;
                        }

                    }
                    key.reset();
                }
            } catch (IOException | InterruptedException e) {
                LOG.error("Error occurred while trying to watch changes in folder: " + ConstantUtils.getPathFolderAppData());
            }
            return null;
        }
    };

    // run the task in a new thread
    Thread th = new Thread(watchingTask);
    th.setDaemon(true);
    th.start();
}