问题描述
我有一个 ListView
,我正在努力将 ContextMenu
添加到。我有 ContextMenu
工作查找,但有另一个问题。
I have a ListView
that I am working to add a ContextMenu
to. I have the ContextMenu
working find but have another issue.
我的 setCellFactory
代码,用于设置上下文菜单:
My setCellFactory
code, used to setup the context menus:
lvAppetites.setCellFactory(lv -> {
ListCell<Appetite> cell = new ListCell<>();
ContextMenu contextMenu = new ContextMenu();
MenuItem editAppetiteMenu = new MenuItem();
editAppetiteMenu.textProperty().bind(Bindings.format("Edit ..."));
editAppetiteMenu.setOnAction(event -> {
// Code to load the editor window
editAppetite(cell.getItem());
});
contextMenu.getItems().add(editAppetiteMenu);
MenuItem deleteAppetiteMenu = new MenuItem();
deleteAppetiteMenu.textProperty().bind(Bindings.format("Delete ..."));
deleteAppetiteMenu.setOnAction(event -> {
// Code to delete the appetite
});
contextMenu.getItems().add(deleteAppetiteMenu);
contextMenu.getItems().add(new SeparatorMenuItem());
MenuItem addAppetiteMenu = new MenuItem();
addAppetiteMenu.textProperty().bind(Bindings.format("Add New ..."));
addAppetiteMenu.setOnAction(event -> {
// Code to delete the appetite
});
contextMenu.getItems().add(addAppetiteMenu);
cell.textProperty().bind(cell.itemProperty().asString());
// If nothing selected, remove the context menu
cell.emptyProperty().addListener((obs, wasEmpty, isNowEmpty) -> {
if (isNowEmpty) {
cell.setContextMenu(null);
} else {
cell.setContextMenu(contextMenu);
}
});
return cell;
});
我的 ListView
可通过<$搜索带听众的c $ c> TextField ;当用户输入时,监听器会过滤 ListView
中的项目。
My ListView
is searchable through a TextField
with a listener; the listener filters the items in the ListView
as the user types.
现在的问题是,作为列表过滤后,任何空单元格现在显示 null
。
The problem now, is that as the list is filtered, any empty cells now display null
.
从另一个,我相信 ListView
仍然是显示已删除单元格的图形。我知道如何通过覆盖 updateItem
方法在ListView中处理它,但是如何从我的 setCellFactory 方法改为?
From reading another question, I'm fairly confident that the
ListView
is still displaying a graphic for the removed cells. I know how to handle that within the ListView by overriding the updateItem
method, but how would I handle this from within my setCellFactory
method instead?
这是可能的还是我需要重构我的整个
ListView
?
Is that even possible or will I need to refactor my entire
ListView
?
一如既往地感谢您的帮助!
Thank you, as always, for all your help!
推荐答案
出现问题从行
cell.textProperty().bind(cell.itemProperty().asString());
当单元格为空时,该项目将为空,因此绑定将(我相信)评估到字符串
null
。
When the cell is empty, the item will be null, so the binding will (I believe) evaluate to the string
"null"
.
尝试测试单元格为空或项目为空的内容,例如
Try something that tests for the cell being empty or the item being null, e.g.
cell.textProperty().bind(Bindings
.when(cell.emptyProperty())
.then("")
.otherwise(cell.itemProperty().asString()));
或(感谢@fabian提炼此版本)
or (thanks to @fabian for refining this version)
cell.textProperty().bind(Bindings.createStringBinding(
() -> Objects.toString(cell.getItem(), ""),
cell.itemProperty()));
这篇关于ListView CellFactory - 如何正确删除单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!