Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何指定使用自定义对象时JavaFX ListView应显示的属性?_Listview_User Interface_Javafx - Fatal编程技术网

如何指定使用自定义对象时JavaFX ListView应显示的属性?

如何指定使用自定义对象时JavaFX ListView应显示的属性?,listview,user-interface,javafx,Listview,User Interface,Javafx,我在FXML文件中定义了一个ListView,它将保存MyCustomData对象。我知道如何告诉它要显示MyCustomData的哪个属性的唯一方法是将以下代码添加到我的控制器: myList.setCellFactory(new Callback<ListView<MyCustomData>, ListCell<MyCustomData>>() { @Override public ListCell<MyCustomData>

我在FXML文件中定义了一个ListView,它将保存MyCustomData对象。我知道如何告诉它要显示MyCustomData的哪个属性的唯一方法是将以下代码添加到我的控制器:

myList.setCellFactory(new Callback<ListView<MyCustomData>, ListCell<MyCustomData>>() {
    @Override
    public ListCell<MyCustomData> call(ListView<MyCustomData> param) {
        return new ListCell<MyCustomData>() {
            @Override
            protected void updateItem(MyCustomData item, boolean empty) {
                super.updateItem(item, empty);
                if(item != null) {
                    setText(item.getMyProperty());
                }
            }
        };
    }
});
myList.setCellFactory(新回调(){
@凌驾
公共ListCell调用(ListView参数){
返回新的ListCell(){
@凌驾
受保护的void updateItem(MyCustomData项,布尔值为空){
super.updateItem(项,空);
如果(项!=null){
setText(item.getMyProperty());
}
}
};
}
});

在FXML中用一行代码替换所有这些凌乱的代码,指定应该显示的属性,这当然很好。这可能吗?

首先请注意,您的计算单元实现有一个bug。您必须在
updateItem(…)
方法中处理所有可能性。在您的实现中,如果单元格当前显示一个项目,然后作为空单元格重新使用(例如,如果删除了项目),则该单元格将不会清除其文本

如果将
回调实现为lambda表达式,而不是匿名内部类,则可以显著减少代码量:

myList.setCellFactory(lv -> new ListCell<MyCustomData>() {
    @Override
    protected void updateItem(MyCustomData item, boolean empty) {
        super.updateItem(item, empty);
        setText(item == null ? null : item.getMyProperty() );
    }
});

public class ListViewPropertyCellFactory<T> 
    implements Callback<ListView<T>, ListCell<T>> {

    private final Function<T, String> property ;

    public ListViewPropertyCellFactory(Function<T, String> property) {
        this.property = property ;
    }

    @Override
    public ListCell<T> call(ListView<T> listView) {
        return new ListCell<T>() {
            @Override
            protected void updateItem(T item, boolean empty) {
                super.updateItem(item, boolean);
                setText(item == null ? null : property.apply(item));
            }
        };
    }
}
myList.setCellFactory(new ListViewPropertyCellFactory<>(MyCustomData::getMyProperty));
public class ListViewPropertyCellFactory {

    public static <T> Callback<ListView<T>, ListCell<T>> of(Function<T, String> property) {
        return lv -> new ListCell<T>() {
            @Override
            protected void updateItem(T item, boolean empty) {
                super.updateItem(item, boolean) ;
                setText(item == null ? null : property.apply(item));
            }
        };
    }
}
myList.setCellFactory(ListViewPropertyCellFactory.of(MyCustomData::getMyProperty));