This question already has answers here:
JavaFX Properties in TableView
                                
                                    (3个答案)
                                
                        
                                4年前关闭。
            
                    
我正在尝试创建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值,相同的方法也能很好地工作,但是会删除title和编译时提及的以下异常:

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()));

最佳答案

ReadOnlyLongWrapperObservableValue<Number>

因此,有两种解决方案:


将列更改为TreeTableColumn<Good, Number> folderId;
创建您自己的ObservableValue<Long>实现。


在大多数情况下,就像您可以执行Number.longValue()一样,1应该就可以了。

09-07 22:14