JavaFX Tutorial - Java TreeView.setShowRoot(boolean value)








Syntax

TreeView.setShowRoot(boolean value) has the following syntax.

public final void setShowRoot(boolean value)

Example

In the following code shows how to use TreeView.setShowRoot(boolean value) method.

import java.util.Arrays;
import java.util.List;
/*from  w w  w.  jav a2 s  . co  m*/
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {
    List<Employee> employees = Arrays.<Employee>asList(
            new Employee("a1", "A"),
            new Employee("a2", "A"),
            new Employee("a3", "A"),
            new Employee("b1", "B"),
            new Employee("b2", "B"),
            new Employee("b3", "B"),
            new Employee("c1", "C"),
            new Employee("c2", "C"),
            new Employee("c3", "C"),
            new Employee("c4", "C"),
            new Employee("e1", "E"));
    TreeItem<String> rootNode = new TreeItem<String>("Root");

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

    @Override
    public void start(Stage stage) {
        rootNode.setExpanded(true);
        for (Employee employee : employees) {
            TreeItem<String> empLeaf = new TreeItem<String>(employee.getName());
            boolean found = false;
            for (TreeItem<String> depNode : rootNode.getChildren()) {
                if (depNode.getValue().contentEquals(employee.getDepartment())){
                    depNode.getChildren().add(empLeaf);
                    found = true;
                    break;
                }
            }
            if (!found) {
                TreeItem depNode = new TreeItem(employee.getDepartment());
                rootNode.getChildren().add(depNode);
                depNode.getChildren().add(empLeaf);
            }
        }
        stage.setTitle("Tree View Sample");
        VBox box = new VBox();
        final Scene scene = new Scene(box, 400, 300);
        scene.setFill(Color.LIGHTGRAY);

        TreeView<String> treeView = new TreeView<String>(rootNode);
        treeView.setShowRoot(true);
        treeView.setEditable(true);
        box.getChildren().add(treeView);
        stage.setScene(scene);
        stage.show();
    }
    public static class Employee {

        private final SimpleStringProperty name;
        private final SimpleStringProperty department;

        private Employee(String name, String department) {
            this.name = new SimpleStringProperty(name);
            this.department = new SimpleStringProperty(department);
        }

        public String getName() {
            return name.get();
        }

        public void setName(String fName) {
            name.set(fName);
        }

        public String getDepartment() {
            return department.get();
        }

        public void setDepartment(String fName) {
            department.set(fName);
        }
    }
}