为什么我在JavaFX上得到java.lang.IllegalStateException"不在FX应用程序线程上"?

lin*_*r85 48 java smack javafx-2

我有一个TableView具有附加侦听器的应用程序,因此一旦检测到更改就会刷新,但问题是我得到了java.lang.IllegalStateException: Not on FX application thread; currentThread = Smack Listener Processor (0).这是我的代码:

/**
 * 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");
                    }
                }
            }
         });
}
Run Code Online (Sandbox Code Playgroud)

它进入resetPage方法,我得到了那个异常.为什么会这样?我该如何解决?提前致谢.

Mar*_*arc 116

无法从非应用程序线程直接更新用户界面.相反,使用Platform.runLater()Runnable对象中的逻辑.例如:

Platform.runLater(new Runnable() {
    @Override
    public void run() {
        // Update UI here.
    }
});
Run Code Online (Sandbox Code Playgroud)

作为lambda表达式:

// Avoid throwing IllegalStateException by running from a non-JavaFX thread.
Platform.runLater(
  () -> {
    // Update UI here.
  }
);
Run Code Online (Sandbox Code Playgroud)

  • 你把这段代码放在哪里?我尝试在实例化舞台并设置舞台时执行此操作,但随后我没有收到错误,但页面返回空白。 (2认同)