【问题标题】:What Makes A JavaFx 2 Task Succeed/Fail是什么让 JavaFx 2 任务成功/失败
【发布时间】:2023-03-27 14:15:02
【问题描述】:

我有一个任务,它执行 Navison 查找并返回 ObservableList 或 Null。
无论查找是否成功并且列表是否填充,任务总是调用失败()而不是成功()!
任何想法为什么会发生这种情况?
这是我的任务:

    public class LoadLocsTask extends Task<Void>{
    Button button;
    @Override protected Void call() throws Exception {

            ObservableList<String> list = application.getValidator().getLocList(button.getText());
            if(list != null){
                locationList.setItems(list);
            }else{
                Dialogs.showErrorDialog(application.getPrimaryStage(),"An error occured while retrieving the loaction list from navision.", "Please contact IS department", "Unable To Populate Location List");
                locationList.setItems(null);
            }
        return null;    
    }

    @Override protected void succeeded() {
        super.succeeded();
        selectProgressIndicator.setVisible(false);
        disableSelected(button);

    }
    @Override protected void failed() {
        super.failed();
        selectProgressIndicator.setVisible(false);
        disableSelected(button);

    }
    @Override protected void scheduled() {
        super.scheduled();
        locationList.getSelectionModel().clearSelection();
        locationList.setItems(null);
        selectProgressIndicator.setVisible(true);
        disableAll();

    }
    @Override protected void cancelled() {
        super.cancelled();
        logger.info("Cancelled");
        locationList.setItems(null);
        selectProgressIndicator.setVisible(true);

    }

这里是 application.getValidator().getLocList(button.getText())

public ObservableList<String> getLocList(String selectedLoc) {
    ObservableList<String> binsOL = null;
    String warehouseCode = null;
    if (selectedLoc.equalsIgnoreCase("location1")) {
        warehouseCode = "LOCATION1";
    } else if (selectedLoc.equalsIgnoreCase("location2")) {
        warehouseCode = "LOCATION2";
    } else if (selectedLoc.equalsIgnoreCase("location3")) {
        warehouseCode = "LOCATION3";
    } else if (selectedLoc.equalsIgnoreCase("location4")) {
        warehouseCode = "LOCATION4";
    } else if (selectedLoc.equalsIgnoreCase("location5")) {
        warehouseCode = "LOCATION5";
    } 

    if (warehouseCode != null) {
        try {
            binsOL = FXCollections.observableArrayList();
            Bins[] bins = navWebservice.getBinsList(warehouseCode);
            for (Bins bin : bins) {
                binsOL.add(bin.getCode());
            }
        } catch (RemoteException e) {
            logger.error("LocationSelectionController - processLocButton(): Could not generate list for " + warehouseCode + " button", e);
            Dialogs.showErrorDialog(parentApp.getPrimaryStage(), e.getMessage(), "Please contact IS department", "Unable To Populate Location List");

        }
    }
    return binsOL;

}

提前致谢!!!

【问题讨论】:

  • @jewelsea 你为什么不把它作为答案?

标签: java multithreading javafx-2 task


【解决方案1】:

您的代码不是线程安全的,如果不将这些操作包装在Platform.runLater 中,您不应该在任务调用方法中显示对话框和更新列表视图中的项目。

有关更多信息,请在 stackoverflow 中搜索 Platform.runLater 和 JavaFX 并发:

【讨论】:

    【解决方案2】:

    在返回语句调用successed()之前的Call方法内部;

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2014-07-31
    • 1970-01-01
    • 2019-07-21
    • 2012-09-13
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多