我正在使用JavaFX作为我正在编写的应用程序的GUI。我的GUI中有一个ListView和一个Button。
我希望ListView在鼠标点击时保持多项选择,并且只在再次点击时删除选择(这样可行)。
我还需要验证ListView中是否已选择某些内容,并在未启用任何内容时禁用该按钮(适用于选择但不取消选择)。
这是按钮启用和禁用的代码(这不起作用):
//Binding used to enable and disable the button
BooleanBinding validEntriesBinding = new BooleanBinding(){
{
super.bind(listView.getSelectionModel().selectedItemProperty());
}
@Override
protected boolean computeValue() {
log.info("No individual item selected? " +
(listView.getSelectionModel().getSelectedItems().isEmpty()));
log.info("Selected items: " + listView.getSelectionModel().getSelectedItems().toString());
return (listView.getSelectionModel().getSelectedItems().isEmpty());
}
};
button.disableProperty().bind(validEntriesBinding);
以下是我捕获鼠标选择的代码(这是有效的,解决方案是从this SO 回答fabian开发的):
//Example sourced from stackoverflow.com/questions/40900478/mimicking-ctrlclick-multiple-selection-in-listview-using-javafx
//Answer by fabian
//Must use MouseEvent.MOUSE_PRESSED and not MOUSE_CLICKED, otherwise the selection does not work
listView.addEventFilter(MouseEvent.MOUSE_PRESSED, event -> {
Node node = event.getPickResult().getIntersectedNode();
while(node != null && node != listView && !(node instanceof ListCell)){
node = node.getParent();
}
if(node instanceof ListCell){
event.consume();
listView.requestFocus();
ListCell cell = (ListCell) node;
if(!cell.isEmpty()){
if(cell.isSelected()){
log.info("Clear selection...");
listView.getSelectionModel().clearSelection(cell.getIndex());
log.info("Selection cleared");
}
else{
log.info("Selecting...");
listView.getSelectionModel().select(cell.getIndex());
log.info("Selected");
}
}
else{
log.warn("Cell is empty... cannot select");
}
}
else{
log.warn("Unable to handle event for " + node.getId() + " " + node.getClass());
}
});
以下是我运行应用程序时的输出结果:
//Before click, button disabled
No individual item selected? true
Selected items: []
//First click selecting a cell, button enabled
Selecting...
No individual item selected? false
Selected items: [SV124]
Selected
//Second click deselecting the first selected cell, button stays
//enabled as the BooleanBinding never fires
Clear selection...
Selection cleared
为什么BooleanBinding
不会触发listView.getSelectionModel().clearSelection(cell.getIndex());
?或者,更直接地说,为什么listView.getSelectionModel().selectedItemProperty()
在选择为空时不会触发?根据Javadoc,它应该在空时返回null,所以不应该被捕获?
答案 0 :(得分:1)
似乎从列表视图的选择模型中删除选定的索引无法使selectedItemProperty()
无效。 (这似乎是一个错误。)
如果您绑定到selectedIndexProperty
而不是selectedItemProperty
:
BooleanBinding validEntriesBinding = new BooleanBinding(){
{
super.bind(listView.getSelectionModel().selectedIndexProperty());
}
@Override
protected boolean computeValue() {
log.info("No individual item selected? " +
(listView.getSelectionModel().getSelectedItems().isEmpty()));
log.info("Selected items: " + listView.getSelectionModel().getSelectedItems().toString());
return (listView.getSelectionModel().getSelectedItems().isEmpty());
}
};
也许,因为您启用了多个选择,所以最好还是绑定到所选项目的列表:
BooleanBinding validEntriesBinding = new BooleanBinding(){
{
super.bind(listView.getSelectionModel().getSelectedItems());
}
@Override
protected boolean computeValue() {
log.info("No individual item selected? " +
(listView.getSelectionModel().getSelectedItems().isEmpty()));
log.info("Selected items: " + listView.getSelectionModel().getSelectedItems().toString());
return (listView.getSelectionModel().getSelectedItems().isEmpty());
}
};
请注意,如果您将validEntriesBinding
设为局部变量,则很容易accidental garbage collection。你应该把它变成一个字段:
private BooleanBinding validEntriesBinding ;
// ...
validEntriesBinding = new BooleanBinding(){
{
super.bind(listView.getSelectionModel().getSelectedItems());
}
@Override
protected boolean computeValue() {
log.info("No individual item selected? " +
(listView.getSelectionModel().getSelectedItems().isEmpty()));
log.info("Selected items: " + listView.getSelectionModel().getSelectedItems().toString());
return (listView.getSelectionModel().getSelectedItems().isEmpty());
}
};