qq_33230192 2020-10-10 16:34 采纳率: 0%
浏览 263

java.lang.IllegalStateException: Not on FX application thread;该怎么解决

在运行程序时出现java.lang.IllegalStateException: Not on FX application thread错误,以下是代码片段

    private void bindingKeyPress() {
        App.clearKeyEvent();
        KeyCombination f9 = new KeyCodeCombination(KeyCode.F9);
        App.putKeyEvent(f9, new Runnable() {
                    @Override
                    public void run() {
                        onPrintData();
                    }
            });
    }

搜索用Platform.runLater(new Runnable() {})来解决,但依旧报相同的问题。代码里的App.putKeyEvent(f9, new Runnable() {})这段话该怎么用Platform.runLater包裹起来?我无论怎么弄都会有红线报错。
以下是controller里的fxml部分代码

@FXML
    private void onPrintData() {
        Alert alert = commonAlert.getProgressIndicatorAlert();
        alert.show();

        Service<String> service = new Service<String>() {
            @Override
            protected Task<String> createTask() {
                Task<String> task = new Task<String>() {
                    @Override
                    protected String call() throws Exception {
                        if (checkbox1.isSelected()) {
                            c1Service.print();
                        }
                        if (checkbox2.isSelected()) {
                            c2Service.print();
                        }
                        if (checkbox3.isSelected()) {
                            c3Service.print();
                        }
                        if (checkbox4.isSelected()) {
                            c4Service.print();
                        }
                        if (checkboxCover.isSelected()) {
                            cCoverService.print();
                        }
                        if (checkboxReportStatement.isSelected()) {
                            cReportService.print();
                        }
                        if (checkboxAuditStatement.isSelected()) {
                            cAuditService.print();
                        }
                        if ((checkboxReportStatement.isSelected())&&(checkboxAuditStatement.isSelected())) {
                            cReportAndAuditService.print();
                        }
                        return "OK";
                    }
                };
                commonTask.setTaskHandling(task, alert);
                return task;
            }
        };
        service.start();
    }

    private void bindingKeyPress() {
        App.clearKeyEvent();
        KeyCombination f9 = new KeyCodeCombination(KeyCode.F9);
        App.putKeyEvent(f9, new Runnable() {
                    @Override
                    public void run() {
                        onPrintData();
                    }
            });


    }

  • 写回答

1条回答 默认 最新

  • 关注
    评论

报告相同问题?