1

我正在尝试创建一个TreeTableView并尝试将销售价值工厂设置为

    @FXML
    private TreeTableColumn<Good, Long> folderId;

这里:

 folderId.setCellValueFactory((TreeTableColumn.CellDataFeatures<Good, Long> param) ->
                new ReadOnlyLongWrapper(param.getValue().getValue().getFolderId()));

我得到这个带有下划线的红色标题中描述的错误。getFolderId 课堂上Good是这样的:

public Long getFolderId() {
        return folderId;
    }

相同的方法适用于 String 或 Boolean 值,但会删除标题中提到的异常以及编译时的以下内容:

incompatible types: bad return type in lambda expression
    javafx.beans.property.ReadOnlyLongWrapper cannot be converted to javafx.beans.value.ObservableValue<java.lang.Long>

如何获取ObservableValue<java.lang.Long>而不是ReadOnlyLongWrapper为什么这个异常只出现在 Long 值中?

这是整个方法:

goodsPane.setCenter(goodTreeTableView);
        List<Good> goodAndFoldersList = goodsService.getGoods();
        List<Good> goods = new ArrayList<Good>();
        List<Good> roots = new ArrayList<Good>();
        for (Good good: goodAndFoldersList){
            if (good.isIs_folder()) {
                roots.add(good);
            }else {
                goods.add(good);
            }
        }
        TreeItem<Good> rootItem = new TreeItem<>();
        for (Good root: roots){
            Long folderId = root.getId();
            TreeItem<Good> rootTreeItem = new TreeItem<>(root);
            for (Good good: goods){
                if (good.getFolderId() == folderId){
                    TreeItem<Good> goodTreeItem = new TreeItem<>(good);
                    rootTreeItem.getChildren().add(goodTreeItem);
                }
            }
            rootItem.getChildren().add(rootTreeItem);
        }
        goodTreeTableView.setRoot(rootItem);
        goodName.setCellValueFactory((TreeTableColumn.CellDataFeatures<Good, String> param) ->
                new ReadOnlyStringWrapper(param.getValue().getValue().getName()));
        folderId.setCellValueFactory((TreeTableColumn.CellDataFeatures<Good, Long> param) ->
                new ReadOnlyLongWrapper(param.getValue().getValue().getFolderId()));
        is_folder.setCellValueFactory((TreeTableColumn.CellDataFeatures<Good, Boolean> param) ->
                new ReadOnlyBooleanWrapper(param.getValue().getValue().isIs_folder()));
4

1 回答 1

3

ReadOnlyLongWrapper是一个ObservableValue<Number>

所以手头有两种解决方案:

  1. 将您的列更改为TreeTableColumn<Good, Number> folderId;
  2. 创建您自己的ObservableValue<Long>.

在大多数情况下, 1 应该没问题,就像你可以做的那样Number.longValue()

于 2015-05-11T16:41:25.277 回答