1 回答
TA贡献1856条经验 获得超5个赞
这是一个简单的解决方法,只需将 keyListener 注册到列表中,然后当您按 Space 时处理 JavaFx 未处理的项目:
代码:
public class Controller implements Initializable {
@FXML private CheckListView<String> list;
@Override
public void initialize(URL location, ResourceBundle resources) {
list.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
list.getItems().addAll("Apple", "Lemon", "Orange", "Banana");
list.setOnKeyPressed(event -> {
if (event.getCode().equals(KeyCode.SPACE)) {
revertCheck(list.getSelectionModel().getSelectedIndices());
}
});
}
private void revertCheck(ObservableList<Integer> selectedIndices) {
selectedIndices.forEach(index -> {
// If needed to skip the selected index which is handled by JavaFx
if (!index.equals(list.getSelectionModel().getSelectedIndex())) {
if (list.getCheckModel().isChecked(index)) {
list.getCheckModel().clearCheck(index);
} else {
list.getCheckModel().check(index);
}
}
});
}
}
.fxml :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import org.controlsfx.control.CheckListView?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="checklist.Controller">
<CheckListView fx:id="list"/>
</AnchorPane>
添加回答
举报