This discussion is archived
8 Replies Latest reply: Jul 12, 2013 1:45 PM by dmv RSS

Get Selected checkboxes from treeView

1005510 Newbie
Currently Being Moderated
Good afternoon,

Please would you provide me with a response,
I have a treeView with checkBoxes, and I want when I click a button to get the selectedItems
I use a cell factory

this is the code :
CheckBoxTreeItem<String> rootItem =
new CheckBoxTreeItem<String>("View Source Files");
rootItem.setExpanded(true);

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

tree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
for (int i = 0; i < 8; i++) {
final CheckBoxTreeItem<String> checkBoxTreeItem =
new CheckBoxTreeItem<String>("Sample" + (i+1));
rootItem.getChildren().add(checkBoxTreeItem);
}


Thank you.
  • 1. Re: Get Selected checkboxes from treeView
    James_D Guru
    Currently Being Moderated
    Keep references to your CheckBoxTreeItems; these have a selected property.
    CheckBoxTreeItem<String> rootItem =
    new CheckBoxTreeItem<String>("View Source Files");
    rootItem.setExpanded(true); 
    final TreeView tree = new TreeView(rootItem); 
    tree.setEditable(true);
    tree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
    
    List<CheckBoxTreeItem<String>> treeItems = new ArrayList<CheckBoxTreeItem<String>>();
    for (int i = 0; i < 8; i++) {
      final CheckBoxTreeItem<String> checkBoxTreeItem =
          new CheckBoxTreeItem<String>("Sample" + (i+1));
      // rootItem.getChildren().add(checkBoxTreeItem); 
      treeItems.add(checkBoxTreeItem);
    }
    rootItem.getChildren().addAll(treeItems);
    
    // Code to determine which items are selected:
    for (CheckBoxTreeItem<String> treeItem : treeItems) {
      if (treeItem.isSelected()) {
        System.out.println(treeItem.getValue() + " is selected");
      }
    }
  • 2. Re: Get Selected checkboxes from treeView
    shakir.gusaroff Expert
    Currently Being Moderated
    Hi. Add a listener to the CheckBoxTreeItems selectd property:
      
              checkBoxTreeItem.selectedProperty().addListener(new ChangeListener<Boolean>() {
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    if(newValue)
                       System.out.println("The selected item is "  + checkBoxTreeItem.valueProperty().get());
                    
                    
                }
            } );
    Here is a working example:
    import javafx.application.Application;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.CheckBoxTreeItem;
    import javafx.scene.control.TreeView;
    import javafx.scene.control.cell.CheckBoxTreeCell;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class TreeCheckBox extends Application {
    
        @Override
        public void start(Stage primaryStage) {
            CheckBoxTreeItem<String> rootItem =
                    new CheckBoxTreeItem<String>("View Source Files");
            rootItem.setExpanded(true);
    
            final TreeView tree = new TreeView(rootItem);
            tree.setEditable(true);
    
    
            tree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
            for (int i = 0; i < 8; i++) {
                final CheckBoxTreeItem<String> checkBoxTreeItem =
                        new CheckBoxTreeItem<String>("Sample" + (i + 1));
    
                checkBoxTreeItem.selectedProperty().addListener(new ChangeListener<Boolean>() {
                    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                        if (newValue) {
                            System.out.println("The selected item is " + checkBoxTreeItem.valueProperty().get());
                        }
                    }
                });
                rootItem.getChildren().add(checkBoxTreeItem);
            }
    
            StackPane root = new StackPane();
            root.getChildren().add(tree);
    
            Scene scene = new Scene(root, 300, 250);
    
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
  • 3. Re: Get Selected checkboxes from treeView
    1005510 Newbie
    Currently Being Moderated
    Thank you most sincerely, It works very well
  • 4. Re: Get Selected checkboxes from treeView
    dmv Newbie
    Currently Being Moderated

    I would like to thank you too. When I foumd your post,I already had a Listener in place but I had no idea how to identify the actual TreeItem from within the Listener. Stuff like this should appear in the documentation.

     

    Thanks again!

  • 5. Re: Get Selected checkboxes from treeView
    dmv Newbie
    Currently Being Moderated

    I can implement the above solution with my application but my TreeView can contain many tens of thousands of CheckBoxTreeItems.

     

    Using the solution above, I will presumably create a separate listener for each item in the tree. If I simply create a single listener and add that same listener to every item in the tree, the above solution is not availale so, in this case, Is there a way of identifying the actual item either as an object or by its index position in the TreeView?

     

    Thank you in advance.

  • 6. Re: Get Selected checkboxes from treeView
    James_D Guru
    Currently Being Moderated

    dmv wrote:

     

    I can implement the above solution with my application but my TreeView can contain many tens of thousands of CheckBoxTreeItems.

     

    Using the solution above, I will presumably create a separate listener for each item in the tree. If I simply create a single listener and add that same listener to every item in the tree, the above solution is not availale so, in this case, Is there a way of identifying the actual item either as an object or by its index position in the TreeView?

     

    Thank you in advance.

     

    Is it an issue to create a separate listener for each item? The listeners will be pretty lightweight compared to the CheckBoxTreeItems, so you shouldn't be massively increasing your memory consumption.

     

    If you really want a single listener, I think the ObservableValue<? extends Boolean> that is passed into the changed(...) method is a reference to the selectedProperty of the CheckBoxTreeItem. So (I think) you could downcast this to a BooleanProperty; calling getBean() on that BooleanProperty (and downcasting that) should return a reference to the CheckBoxTreeItem.

  • 7. Re: Get Selected checkboxes from treeView
    dmv Newbie
    Currently Being Moderated

    Thank you James. I worry about memory for the whole app so every little helps. I'm going to try your suggestion - it is accepted syntactically but I now I have to do a little hooking up. I will report back ASAP.

  • 8. Re: Get Selected checkboxes from treeView
    dmv Newbie
    Currently Being Moderated

    Many thanks, James. That was excellent advice and it works perfectly. Hopefully it will help others with the same problem. I've actually extended CheckBoxTreeIitem so your solution is really neat for my purpose. Thank goodness guys like you give your time to the questions on this forum.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points