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>
添加回答
舉報