Question

Whenever I instantiate a CheckBoxTreeView, it requires a TreeItem with no selected children TreeItems to be unselected. How do I change that? I tried calling setIndependent(true) on the parent TreeItem, but it still must have atleast one checked child TreeItem to be checked.

I tried making a custom TreeCell with checkboxes as the graphic, but couldn't figure out how to keep the item from resetting. This problem is posted here: https://stackoverflow.com/questions/17606280/javafx-2-checkbox-in-treecell-keeps-resetting-cant-bind-it

Was it helpful?

Solution

I think your problem is that when you expand or resume part of the tree the TreeView creates brand new CustomCheckBoxCell which are unselected by default. You could keep track of your CustomCheckBoxCell in an array and give them to the TreeView when it needs them. I could not manage to solve it.

However I might sound a little obvious, and you may already tried that, but why not use TreeItem<CheckBox> objects and add them to your TreeView ?

I tried to reproduce your problem in the following code, tell me if I got it right.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.CheckBoxTreeItem;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


public class CheckBoxTreeView extends Application{

    @Override
    public void start(Stage primaryStage) throws Exception {

        CheckBoxTreeItem<CheckBox> rootItem = 
                new CheckBoxTreeItem<CheckBox>(new CheckBox("root"));
        rootItem.setExpanded(true);                  

        final TreeView<CheckBox> tree = new TreeView<CheckBox>(rootItem);  
        tree.setEditable(true);

        for (int i = 0; i < 8; i++) {

            System.out.println("new tree Item");
            final TreeItem<CheckBox> checkBoxTreeItem = 
                    new TreeItem<CheckBox>(new CheckBox(GeoObj.toString()));
            rootItem.getChildren().add(checkBoxTreeItem);                
        }

        tree.setRoot(rootItem);
        System.out.println("root item set");
        tree.setShowRoot(true);

        StackPane root = new StackPane();
        root.getChildren().add(tree);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

The first letter of your Types must be an uppercase according to good principles ... Then geoObj -> GeoObj customCheckBoxCell->CustomCheckBoxCell

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top