为什么我会收到 java.lang.IllegalStateException “Not on FX application thread"?在 JavaFX 上? [英] Why am I getting java.lang.IllegalStateException "Not on FX application thread" on JavaFX?
问题描述
我有一个应用程序,它有一个 TableView
,它有一个附加的侦听器,因此它会在检测到更改时立即刷新,但问题是我正在获取 java.lang.IllegalStateException:不在 FX 应用程序线程上;currentThread = Smack 侦听器处理器 (0)
.这是我的代码:
I have an application that has a TableView
that has an attached listener so it refreshes as soon as it detects a change, but the thing is that I´m getting java.lang.IllegalStateException: Not on FX application thread; currentThread = Smack Listener Processor (0)
.
Here is my code:
/**
* This function resets the pagination pagecount
*/
public void resetPage() {
try {
System.out.println("RESET");
int tamRoster = this.loginManager.getRosterService().getRosterList().size();
paginationContactos.setPageCount((int)(Math.ceil(tamRoster*1.0/limit.get())));
int tamEnviados = this.loginManager.getRosterService().getEnviadasList().size();
paginationEnviadas.setPageCount((int)(Math.ceil(tamEnviados*1.0/limit.get())));
int tamRecibidas = this.loginManager.getRosterService().getRecibidasList().size();
paginationRecibidas.setPageCount((int)(Math.ceil(tamRecibidas*1.0/limit.get())));
} catch (Exception e) {
e.printStackTrace();
}
}
public void doSomething () {
this.loginManager.getRosterService().getRosterList().addListener(new ListChangeListener<RosterDTO>() {
@Override
public void onChanged(
javafx.collections.ListChangeListener.Change<? extends RosterDTO> c) {
// TODO Auto-generated method stub
resetPage();
while (c.next()) {
if (c.wasPermutated()) {
System.out.println("PERM");
} else if (c.wasUpdated()) {
System.out.println("UPD");
} else {
System.out.println("ELSE");
}
}
}
});
}
尽管它进入了 resetPage 方法,但我得到了那个异常.为什么会这样?我该如何解决?提前致谢.
Altough it enters the resetPage method, I get that exception. Why is this happening? How can I fix it? Thanks in advance.
推荐答案
不能从非应用程序线程直接更新用户界面.相反,使用 Platform.runLater()
和 Runnable 对象内部的逻辑.例如:
The user interface cannot be directly updated from a non-application thread. Instead, use Platform.runLater()
, with the logic inside the Runnable object. For example:
Platform.runLater(new Runnable() {
@Override
public void run() {
// Update UI here.
}
});
作为 lambda 表达式:
As a lambda expression:
// Avoid throwing IllegalStateException by running from a non-JavaFX thread.
Platform.runLater(
() -> {
// Update UI here.
}
);
这篇关于为什么我会收到 java.lang.IllegalStateException “Not on FX application thread"?在 JavaFX 上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!