0

编辑:我不明白我如何从任务中更改 UI,我使用控制器的方法来更改 UI,我该怎么做?请尝试使用我的示例并告诉我是否需要添加一些信息

我正在尝试使用控制器类中的方法将文本从任务添加到文本流,由于某种原因,程序在 .getChildren() 方法上失败。

在控制器类中调用 Spliter:

btnSplit.setOnMouseClicked(new EventHandler<Event>() {
            @Override
            public void handle(Event event) {
                Thread split = new Thread(new Spliter(custToSplit, Controller.this));
                split.setDaemon(true);
                split.start();

            }
        });

类拆分器构造函数:

public Spliter(File f, Controller c){
        this.c = c;
        this.cust = f;

    }
c.updateHebFlow("Dir created: "+ newDir.getAbsolutePath() , INFO_TEXT);

控制器类的一部分:

@FXML
    private TextFlow hebFlow;
@Override
    public void initialize(URL location, ResourceBundle resources) {
assert hebFlow != null : "fx:id=\"hebFlow\" was not injected: check your FXML file 'MainXml.fxml'.";

    public void updateHebFlow(String text,String type){
        normalText = new Text();
        errorText = new Text();
        errorText.setFill(Color.RED);
        infoText = new Text();
        infoText.setFill(Color.BLUE);
        switch(type){
        case(ERROR_TEXT) :
            errorText.setText(text);
            hebFlow.getChildren().addAll(new Text("/n"), errorText);
            break;
        case(INFO_TEXT) :
            infoText.setText(text);
            hebFlow.getChildren().addAll(new Text("/n"), infoText);
            break;
        case(NORMAL_TEXT) :
            normalText.setText(text);
            hebFlow.getChildren().addAll(new Text("/n"), normalText);
            break;
        }
    }   
}

该程序在没有 Textflow 的情况下运行良好,它在 getChildren() 方法处停止。这里的错误在哪里?

编辑:感谢@James_D,我忘了在任务中捕获异常。我在 spliter 中添加了方法调用:

try{
c.updateHebFlow("Script by TulTul", INFO_TEXT);
}catch (Exception e){
            e.printStackTrace();
        }

我得到:

java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
4

0 回答 0