[英]JavaFX Binding String to TreeCell Factory Properly
I'm getting an issue, where when I expand and unexpand a tree cell. 我遇到一个问题,当我展开和展开树单元时。 The text I binded to doesn't ever go away.. What could be the cause of this? 我绑定的文本永远不会消失。.这可能是什么原因?
Here is my code 这是我的代码
treeView.setCellFactory(t -> {
return new TreeCell<Entry>() {
@Override
protected void updateItem(Entry entry, boolean empty) {
super.updateItem(entry, empty);
if (entry != null) {
if (empty) {
setText(null);
setGraphic(null);
} else {
if (entry.getImage() != null) {
Image image = FXUtils.toFXImage(entry.getImage());
setGraphic(new ImageView(image));
} else {
setGraphic(null);
}
textProperty().bind(entry.nameProperty());
}
}
}
};
});
If a cell is empty, the item is also null
. 如果单元格为空,则该项也为null
。 However all your logic is only executed, if (entry != null)
, ie never, if the cell is empty or the item is null
. 但是,仅当if (entry != null)
时才执行所有逻辑,即,如果单元格为空或item为null
,则永远不会执行。 Furthermore this should fail, even if you fix this error, since you never unbind the textProperty
, but it could be set for empty cells. 此外,即使您修复了此错误,此操作也将失败,因为您从未取消绑定textProperty
,但是可以为空单元格设置它。 Change your updateItem
method to something like this: 将您的updateItem
方法更改为如下所示:
@Override
protected void updateItem(Entry entry, boolean empty) {
super.updateItem(entry, empty);
textProperty().unbind();
if (empty || entry == null) {
setText(null);
setGraphic(null);
} else {
if (entry.getImage() != null) {
Image image = FXUtils.toFXImage(entry.getImage());
setGraphic(new ImageView(image));
} else {
setGraphic(null);
}
textProperty().bind(entry.nameProperty());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.