gpt4 book ai didi

JavaFX 卡住窗口

转载 作者:行者123 更新时间:2023-12-01 18:32:05 25 4
gpt4 key购买 nike

当我过滤 TreeView (长过程)时遇到问题,我的应用程序卡住了。我尝试在单独的线程(Thread)中执行此操作,但随后出现错误“不在 FX 应用程序线程上;currentThread = Thread-5”

void InitBtnFind() {
//Event Button Search
btnFind.setOnAction(event -> {
newFind();
if (Config.isRoot()) {
String finalSFilterExt = filterExt.getText();
String finalSearchW = searchWord.getText();
Platform.runLater(() -> {
try {
// imitation of work
Thread.sleep(5000);
fileView.setRoot(treeView.filterChanged(finalSFilterExt, finalSearchW));
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
System.out.println("lol");
});
}
});
}

您可以提供示例代码来解决我的问题。附注不要降低我的声誉,我对这件事很感兴趣

P.s.我尝试用线程来做到这一点

//Event Button Search
btnFind.setOnAction(event -> {
newFind();
if (Config.isRoot()) {
String finalSFilterExt = filterExt.getText();
String finalSearchW = searchWord.getText();
if (findThread != null && findThread.isAlive())
findThread.interrupt();
findThread = new Thread(() -> {
try {
fileView.setRoot(treeView.filterChanged(finalSFilterExt, finalSearchW));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("lol");
});
findThread.setName("findThread");
findThread.setDaemon(true);
findThread.start();
}
System.out.println("kek");
});

最佳答案

我是这样修复的,我不知道它有多正确。但这对我有用。我希望它会有用。

  //Event Button Search
btnFind.setOnAction(event -> {
newFind();
if (Config.isRoot()) {
String finalSFilterExt = filterExt.getText();
String finalSearchW = searchWord.getText();
if (findThread != null && findThread.isAlive())
findThread.interrupt();
findThread = new Thread(() -> {
try {
Thread.sleep(2000);
System.out.println("++++++++++++++");
var q =treeView.filterChanged(finalSFilterExt, finalSearchW);
Platform.runLater(()->{fileView.setRoot(q);});
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
});
findThread.setName("findThread");
findThread.setDaemon(true);
findThread.start();
}
});

关于JavaFX 卡住窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60143829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com