java-具有通用标签的Listview setCellFactory
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-具有通用标签的Listview setCellFactory,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1416字,纯文字阅读大概需要3分钟。
内容图文
![java-具有通用标签的Listview setCellFactory](/upload/InfoBanner/zyjiaocheng/667/e0007ad781e24b239a6e79af3a3b9b61.jpg)
我尝试在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();
}
解决方法:
如果确实要显示扩展Node的项目,则无需使用自定义ListCell.默认工厂的ListCell已在执行此操作.
但是,在这种情况下,您将调用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;
}
});
内容总结
以上是互联网集市为您收集整理的java-具有通用标签的Listview setCellFactory全部内容,希望文章能够帮你解决java-具有通用标签的Listview setCellFactory所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。