我有一个 TableView
有一些行。用户可以选择任何行,但当他单击空行或 Stage
上的任何位置时,我要清除他当前选择的TableView
.
最佳答案
您可以将事件过滤器添加到 Scene
使用 TableView
的选择模型清除选择,如果点击是在空行或 TableView
之外的任何地方:
scene.addEventFilter(MouseEvent.MOUSE_CLICKED, evt -> {
Node source = evt.getPickResult().getIntersectedNode();
// move up through the node hierarchy until a TableRow or scene root is found
while (source != null && !(source instanceof TableRow)) {
source = source.getParent();
}
// clear selection on click anywhere but on a filled row
if (source == null || (source instanceof TableRow && ((TableRow) source).isEmpty())) {
tableView.getSelectionModel().clearSelection();
}
});
关于javafx - 单击 javafx 中的空行时清除 TableView 中的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41907705/