0

I'm using CheckTreeView to represent hierachical structure. The current checking behaviour is if I check/uncheck a parent, all of its children will automatically get check/unchecked. If I check/uncheck all the children the parent also gets automatically checked/unchecked. Is there any way to remove this behaviour? I want to be able to check all the children without changing the checked state of the parent to true. I also want to be able to check the parent and some or no children but not all of them. In short I want the check boxes to be totally independent from the others. I have attached an edited screenshot to demonstrate what I describe

This modified screenshot is what I want to achieve

Example code to build the tree:

@FXML
private void initialize() {
    CheckBoxTreeItem<String> parent1 = new CheckBoxTreeItem<>("Parent 1");
    addChildren(parent1);
    CheckBoxTreeItem<String> parent2 = new CheckBoxTreeItem<>("Parent 2");
    addChildren(parent2);
    CheckBoxTreeItem<String> root = new CheckBoxTreeItem<>("Root");
    root.getChildren().addAll(parent1, parent2);
    ctv.setRoot(root);
}

private void addChildren(TreeItem<String> parent) {
    parent.getChildren().add( new CheckBoxTreeItem<>("Child 1"));
    parent.getChildren().add( new CheckBoxTreeItem<>("Child 2"));
}
Gnas
  • 698
  • 1
  • 6
  • 14

1 Answers1

0

By default CheckBoxTreeItems are dependent.

To change the default value, use the setIndependent() call:

parent1.setIndependent(true);
parent2.setIndependent(true);

And the same for children:

CheckBoxTreeItem<String> child1 = new CheckBoxTreeItem<>("Child 1");
child1.setIndependent(true);
parent.getChildren().add(child1);
CheckBoxTreeItem<String> child2 = new CheckBoxTreeItem<>("Child 2");
child2.setIndependent(true);
parent.getChildren().add(child2);

See API: JavaFX Javadocs

Perdi Estaquel
  • 819
  • 1
  • 6
  • 21