TreeView with TreeItem : TreeView « JavaFX « Java






TreeView with TreeItem

 
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();
  }
}

   
  








Related examples in the same category

1.Create TreeView from Bean
2.Create the tree item on the fly