JavaFX Tutorial - Java TreeView(TreeItem < T > root) Constructor








Syntax

TreeView(TreeItem < T > root) constructor from TreeView has the following syntax.

public TreeView(TreeItem < T > root)

Example

In the following code shows how to use TreeView.TreeView(TreeItem < T > root) constructor.

/*from  w ww. j  a  v  a  2  s .c  om*/
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {
  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    stage.setTitle("Button Sample");
    stage.setWidth(300);
    stage.setHeight(190);

    VBox vbox = new VBox();
    vbox.setLayoutX(20);
    vbox.setLayoutY(20);
    
    TreeItem<String> root = new TreeItem<String>("Root Node");
    root.setExpanded(true);
    root.getChildren().addAll(
        new TreeItem<String>("Item 1"),
        new TreeItem<String>("Item 2"),
        new TreeItem<String>("Item 3")
    );
    TreeView<String> treeView = new TreeView<String>(root);
    

    vbox.getChildren().add(treeView);
    vbox.setSpacing(10);
    ((Group) scene.getRoot()).getChildren().add(vbox);

    stage.setScene(scene);
    stage.show();
  }
}