为什么我在JavaFX上得到java.lang.IllegalStateException

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

我有一个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");
                    }
                }
            }
         });}

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


摇曳的蔷薇
浏览 1077回答 2
2回答

有只小跳蛙

无法从非应用程序线程直接更新用户界面。相反,使用Platform.runLater()Runnable对象中的逻辑。例如:Platform.runLater(new&nbsp;Runnable()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;@Override &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;run()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Update&nbsp;UI&nbsp;here. &nbsp;&nbsp;&nbsp;&nbsp;}});作为lambda表达式://&nbsp;Avoid&nbsp;throwing&nbsp;IllegalStateException&nbsp;by&nbsp;running&nbsp;from&nbsp;a&nbsp;non-JavaFX&nbsp;thread.Platform.runLater( &nbsp;&nbsp;()&nbsp;->&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Update&nbsp;UI&nbsp;here. &nbsp;&nbsp;});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java