I'm having a TreeView with a few nodes having CheckBoxes (see MWE).
When collapsing / expanding some node, the CheckBoxes of other nodes are checked or unchecked.
To reproduce the behaviour, just expand all the nodes, check ChildA
, collapse Block1
and ChildC
will be automatically checked.
package treeviewexample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.CheckBoxTreeCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TreeViewExample extends Application {
@Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
/* example Treeview */
TreeView tw = new TreeView();
TreeItem rootNode = new TreeItem("Root");
TreeItem blockOne = new TreeItem("Block1");
TreeItem childA = new TreeItem("ChildA");
TreeItem childB = new TreeItem("ChildB");
blockOne.getChildren().add(childA);
blockOne.getChildren().add(childB);
TreeItem blockTwo = new TreeItem("Block2");
TreeItem childC = new TreeItem("ChildC");
TreeItem childD = new TreeItem("ChildD");
blockTwo.getChildren().add(childC);
blockTwo.getChildren().add(childD);
rootNode.getChildren().add(blockOne);
rootNode.getChildren().add(blockTwo);
tw.setRoot(rootNode);
/* add CheckBoxes */
tw.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
root.getChildren().add(tw);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
How can I prevent this behaviour? At a later point in my program, I want to go through the TreeView and get the status (checked or not) of the nodes to work with them.
From https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TreeCell.html I know the following:
Due to the fact that TreeCell extends from IndexedCell, each TreeCell also provides an index property. The index will be updated as cells are expanded and collapsed, and therefore should be considered a view index rather than a model index.
So is this intended behaviour? Why would anyone want to have that?