日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

java目录更改当前_Java-MVC:查看目录更改的最佳方法

發(fā)布時間:2023/11/27 生活经验 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java目录更改当前_Java-MVC:查看目录更改的最佳方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我將使用的方法是在WatchDir中提供用于注冊回調的方法.最簡單的方法是對回調使用

Consumer屬性:

public class WatchDir {

private Consumer onCreate ;

public void setOnCreate(Consumer onCreate) {

this.onCreate = onCreate ;

}

// other callbacks as needed...

// ...

void processEvents() {

// ...

Path child = ... ; // file/folder that was created

if (! recursive && kind == ENTRY_CREATE) {

if (onCreate != null) {

onCreate.accept(child);

}

}

// ...

}

// ...

}

請注意,WatchDir.processEvents()是(非終止)阻塞調用,因此您需要在后臺線程中運行它.因此,您可以從控制器中執(zhí)行以下操作:

WatchDir watchDir = new WatchDir(rootPath, false) ;

watchDir.setOnCreate(path ->

Platform.runLater(() -> listView.getItems().add(path)));

Thread watchThread = new Thread(watchDir::processEvents);

watchThread.setDaemon(true);

watchThread.start();

請注意,由于回調將在后臺線程上調用,因此對UI的更新應包裝在Platform.runLater(…)中.如果愿意,可以為WatchDir配備執(zhí)行程序來執(zhí)行回調,這使您僅需告訴一次即可始終通過Platform.runLater(…)執(zhí)行回調:

public class WatchDir {

// Executor for notifications: by default just run on the current thread

private Executor notificationExecutor = Runnable::run ;

public void setNotificationExecutor(Executor notificationExecutor) {

this.notificationExecutor = notificationExecutor ;

}

private Consumer onCreate ;

// etc...

void processEvents() {

// ...

if (! recursive && kind == ENTRY_CREATE) {

if (onCreate != null) {

notificationExecutor.execute(() -> onCreate.accept(child));

}

}

// ...

}

}

接著

WatchDir watchDir = new WatchDir(rootPath, false) ;

watchDir.setNotificationExecutor(Platform::runLater);

watchDir.setOnCreate(listView.getItems()::add); /* or path -> listView.getItems().add(path) */

Thread watchThread = new Thread(watchDir::processEvents);

watchThread.setDaemon(true);

watchThread.start();

總結

以上是生活随笔為你收集整理的java目录更改当前_Java-MVC:查看目录更改的最佳方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。