问题描述
我正在尝试使用 javafx 将元素放在列表视图和树视图上,但两个控件都不会刷新它们的内容.我使用 obvservable 列表来控制项目,每次我删除一个项目时,列表视图或树视图都会将它从数据源中删除.但视图没有更新.我仍然看到所有的项目.唯一的区别是,被移除的项目不能再被选中.例如链接 2 显示折叠的项目列表.图 1 显示了折叠之前的项目.项目已折叠,但旧条目仍然可见.有没有人知道这个问题的解决方案.谢谢大家帮我
i am trying to put elements on a listview and treeview with javafx, but both controls wont refresh theyre content. i am using an obvservable list to control the items and every time i delete one item, the listview or treeview removes it from the datasource. but the view is not updating. i am still seeing all the items. the only difference is, the removed item can not be selected any more. for example link 2 shows the collaped item list. image 1 shows the items before they are collaped. the items are collapsed but the old entry is still visible. does anybody know a solution for this problem. thank you all for helping me
链接 1:treeview 未折叠链接 2:treeview 已折叠但未更新旧视图
这是我用来显示列表视图的自定义单元工厂:
this is the custom cell factory i use to display a listview:
public ListCell<T> call(final ListView<T> param) {
ListCell<T> cell = new ListCell<T>(){
@Override
protected void updateItem(final T persistentObject, final boolean empty) {
super.updateItem(persistentObject, empty);
if(persistentObject instanceof POProcessStep){
POProcessStep poProcessStep = (POProcessStep) persistentObject;
if (persistentObject != null) {
super.setText(poProcessStep.getId() + " - " + poProcessStep.getTitle());
}
}else if(persistentObject instanceof POProcess){
POProcess poProcess = (POProcess) persistentObject;
if (persistentObject != null) {
super.setText(poProcess.getId() + " - " + poProcess.getTitle());
}
}else if(persistentObject instanceof POCategory){
POCategory poCategory = (POCategory) persistentObject;
if(persistentObject != null){
super.setText(poCategory.getId() + " - " + poCategory.getTitle());
}
}else if(persistentObject instanceof String){
if(persistentObject != null){
super.setText(String.valueOf(persistentObject));
}
}
super.setGraphic(null);
}
};
return cell;
}
推荐答案
你的cell factory的updateItem(...)
需要处理cell为空的情况.当一个项目被移除(或者因为 TreeView
中的一个节点被折叠而变成空)并且之前显示一个项目的单元格被重新用作空单元格时,这将是完全正确的场景:
Your cell factory's updateItem(...)
needs to handle the case where the cell is empty. This will be exactly the scenario when an item is removed (or becomes empty because a node in the TreeView
was collapsed) and the cell that previously showed an item is reused as an empty cell:
public ListCell<T> call(final ListView<T> param) {
ListCell<T> cell = new ListCell<T>(){
@Override
protected void updateItem(final T persistentObject, final boolean empty) {
super.updateItem(persistentObject, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
// ... rest of your code.
}
}
}
return cell ;
}
这篇关于javafx 列表视图和树视图控件未正确重绘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!