我尝试在 中创建一个自定义项目ListView,假设它是标签并且我想执行setCellFactory,而我在使用时Label我没有看到该项目(标签的文本),为什么?
ListView<Label> list = new ListView<Label>();
ObservableList<Label> data = FXCollections.observableArrayList(
new Label("123"), new Label("45678999"));
@Override
public void start(Stage stage) {
VBox box = new VBox();
Scene scene = new Scene(box, 200, 200);
stage.setScene(scene);
stage.setTitle("ListViewExample");
box.getChildren().addAll(list);
VBox.setVgrow(list, Priority.ALWAYS);
list.setItems(data);
list.setCellFactory(new Callback<ListView<Label>, ListCell<Label>>() {
@Override
public ListCell<Label> call(ListView<Label> list) {
ListCell<Label> cell = new ListCell<Label>() {
@Override
public void updateItem(Label item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setItem(item);
}
}
};
return cell;
}
}
);
stage.show();
}
Run Code Online (Sandbox Code Playgroud)
如果您确实想显示扩展的项目,Node则无需使用 custom ListCell。ListCell默认工厂的 s 已经这样做了。
但是,在您的情况下,当单元格变setItem空时,您正在调用而不是,setGraphic并且您也不将属性设置回:null
list.setCellFactory(new Callback<ListView<Label>, ListCell<Label>>() {
@Override
public ListCell<Label> call(ListView<Label> list) {
ListCell<Label> cell = new ListCell<Label>() {
@Override
public void updateItem(Label item, boolean empty) {
super.updateItem(item, empty);
// also sets to graphic to null when the cell becomes empty
setGraphic(item);
}
};
return cell;
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3339 次 |
| 最近记录: |