为什么我得到java.lang.IllegalStateException“不在FX应用程序线程上”在JavaFX上? [英] Why am I getting java.lang.IllegalStateException "Not on FX application thread" on JavaFX?

查看:534
本文介绍了为什么我得到java.lang.IllegalStateException“不在FX应用程序线程上”在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“不在FX应用程序线程上”在JavaFX上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆