【问题标题】:Why am I getting java.lang.IllegalStateException "Not on FX application thread" on JavaFX?为什么我在 JavaFX 上收到 java.lang.IllegalStateException“不在 FX 应用程序线程上”?
【发布时间】:2013-07-24 21:13:02
【问题描述】:

我有一个应用程序,它有一个 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 方法,但我得到了那个异常。 为什么会这样? 我该如何解决? 提前致谢。

【问题讨论】:

    标签: java javafx-2 smack


    【解决方案1】:

    用户界面不能直接从非应用程序线程更新。相反,使用 Platform.runLater(),将逻辑放在 Runnable 对象中。例如:

    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            // Update UI here.
        }
    });
    

    作为 lambda 表达式:

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

    【讨论】:

    • 你把这段代码放在哪里?当我实例化我的舞台并设置舞台时,我尝试这样做,但后来我没有收到错误,但页面返回空白。
    【解决方案2】:

    JavaFX 代码允许从 JavaFX 应用程序线程更新 UI。但是从上面的异常消息中可以看出它没有使用 FX 应用程序线程。

    您可以修复的一种方法是从 resetPage 方法启动 FX 应用程序线程并在那里进行修改。

    【讨论】:

    • 能否详细说明如何启动 FX 应用程序线程?
    【解决方案3】:

    我有一个类似的问题,我使用Platform.runLater() 修复了

    "java.lang.IllegalStateException: 不在 FX 应用程序线程上;currentThread = Thread-6"

    Button play = new Button("Play");
    EventHandler<ActionEvent> playHandler = e1 -> {
        Runnable runnable = () -> {
    
            play.setText("Pause");
    
            EventHandler<ActionEvent> timelineHandler e2 -> play();
            timeline = new Timeline(new KeyFrame(Duration.millis(2000), timelineHandler));
            timeline.setCycleCount(Timeline.INDEFINITE);
            timeline.play();
    
        };
        Thread t = new Thread(runnable);
        t.start();
    };
    play.setOnAction(playHandler);
    

    已解决! 将所有不相关的内容移到runnable lambda 之外:

    Button play = new Button("Play");
    EventHandler<ActionEvent> playHandler = e1 -> {
        play.setText("Pause");
        Runnable runnable = () -> {
    
            EventHandler<ActionEvent> timelineHandler e2 -> play();
            timeline = new Timeline(new KeyFrame(Duration.millis(2000), timelineHandler));
            timeline.setCycleCount(Timeline.INDEFINITE);
            timeline.play();
    
        };
        Thread t = new Thread(runnable);
        t.start();
    };
    play.setOnAction(playHandler);
    

    【讨论】:

      猜你喜欢
      • 2019-08-10
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-24
      • 1970-01-01
      相关资源
      最近更新 更多