Issue
Problem Description
I have a CheckListView with several items(checkboxes). I changed the Selection mode (code below) to allow multiple selections. However, when I select multiple rows, like in the picture below, and press SPACE, only the 'current selected row' changes state.
I want/need to : when I press space, toggle all selected row states.
I tried looking into handlers but im confused to what to change.
Any help is greatly appreciated.
@FXML
private CheckListView<String> checkListPermissoes;
@Override
public void initialize(URL url, ResourceBundle rb) {
... checkListPermissoes.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
Solution
Here is a simple workaround how to do it, simply register a keyListener to the list and when you press Space handle the items which are not handled by JavaFx:
The code:
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>
Answered By - Sunflame
Answer Checked By - Pedro (JavaFixing Volunteer)