javafx how to expand entire treeItem on button click event?


Question

In Javafx, How to expand entire treeItem on button click event ?

I am trying to replicate accordion effect using TreeItem due to some issues in scrolling while using accordion. Please look at my code snippet below-

final TreeItem<FlowPane> rootItem = new TreeItem<>();
root.setExpanded(true);
for (int i = 0; i < 12; i++) {
    TreeItem<FlowPane> rootItem1 = new TreeItem<>(miniflowPane());
    TreeItem<FlowPane> item1 = new TreeItem<>(flowPaneLarge());
    rootItem1.getChildren().add(item1);
    rootItem.setExpanded(false);
}
final TreeView<FlowPane> tree;
tree = new TreeView<>(rootItem);
tree.setShowRoot(false);
tree.setMinWidth(450.0);
tree.setMaxWidth(450.0);
HBox listHeaderBox = new HBox();
listHeaderBox.setAlignment(Pos.CENTER_LEFT);
Button b = new Button(">");
b.setOnAction(new EventHandler<ActionEvent>() {        
@Override
 public void handle(ActionEvent event) {

 }
 });
 listHeaderBox.getChildren().add(b);
 VBox listVBox = new VBox(5.0);
 listVBox.getCHildren().addAll(listHeaderBox,tree);
 Scene scene = new Scene(listVBox,500,650);
 primartStage.setScene(scene);
 primaryStage.show();
}

miniflowPane() and flowPane() functions creates some dummy contents and returns FlowPane. In Button b's click event, how to expand all the children and grandchildren of rootItem ?

1
2
11/27/2014 9:13:18 AM

Accepted Answer

How about this:

private void expandTreeView(TreeItem<?> item){
    if(item != null && !item.isLeaf()){
        item.setExpanded(true);
        for(TreeItem<?> child:item.getChildren()){
            expandTreeView(child);
        }
    }
}

And now add the call in your button handler:

    Button bExpand = new Button(">");
    bExpand.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            expandTreeView(rootItem);
        }
    });

And if you want to add a collapse button too:

    Button bCollapse = new Button("<");
    bCollapse.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            collapseTreeView(rootItem);
        }
    });

where:

private void collapseTreeView(TreeItem<?> item){
    if(item != null && !item.isLeaf()){
        item.setExpanded(false);
        for(TreeItem<?> child:item.getChildren()){
            collapseTreeView(child);
        }
    }
}

EDIT

If the root is not visible, calls can be made from its first children:

    bExpand.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            for(TreeItem<?> child: rootItem.getChildren()){
                expandTreeView(child);
            }
        }
    });
    bCollapse.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            for(TreeItem<?> child: rootItem.getChildren()){
                collapseTreeView(child);
            }
        }
    });
4
11/28/2014 8:50:19 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon