2

我有一个任务是进行 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;

}

提前致谢!!!

4

2 回答 2

1

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

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

于 2017-01-09T17:45:03.477 回答
0

在返回语句之前的 Call 方法中调用 succeeded();

于 2021-11-23T20:08:33.217 回答