mpowell48 mpowell48 - 2 days ago 5
Java Question

Check all CheckBoxTreeCells in a TreeView JavaFX

I have created a

TreeView
that contains many
CheckBoxTreeCells
. I also have a
Button
that I would like to check all of the
CheckBoxTreeCells
. I've been reading this tutorial, and I am a bit lost. Here is what I have so far:

Button Code:

public void fooMethod() {
/*selectAll is an instance variable*/
selectAll = new Button("Select All");
selectAll.setOnMouseClicked(e -> handleSelectAllButtonAction(e));
}

private void handlSelectAllButtonAction(MouseEvent e) {
/*Code goes here*/
}


TreeView Code:

public void fooMethod2() {
/*myTreeView is also an insance variable*/
myTreeView = new TreeView<String>();

CheckBoxTreeItem<String> root = new CheckBoxTreeItem<>();
CheckBoxTreeItem<String> branch1 = new CheckBoxTreeItem<>("Branch 1");
CheckBoxTreeItem<String> branch2 = new CheckBoxTreeItem<>("Branch 2");
CheckBoxTreeItem<String> branch3 = new CheckBoxTreeItem<>("Branch 3");

root.getChildren.add(branch1);
root.getChildren.add(branch2);
root.getChildren.add(branch3);

myTreeView.setCellFactory(CheckBoxTreeCell.forTreeView());
myTreeView.setRoot(root);
myTreeView.setShowRoot(false);
myTreeView.setEditable(true);
}


The example provided in the link is a bit more complex than what I need, and I think it is confusing me. How do I edit a CheckBoxTreeItems in a TreeView?

Answer

Unless there are independent CheckBoxTreeItems, its enough to select the root:

root.setSelected(true);

since this automatically selects the children.

Comments