Monday, 18 June 2018

JavaFX: TreeTableView: Create Tree Table

In this post, I am going to show you how to create TreeTableView.

Below step by step procedure explains how to create TreeTableView widget.

Step 1: Create TreeItems.
                  /* Define Root Item */
                  TreeItem<String> rootItem = new TreeItem<>("College");
                  rootItem.setExpanded(true);

                  /* Defile 1st level child items */
                  TreeItem<String> physicsDepartment = new TreeItem<>("Physics Department");
                  TreeItem<String> chemistryDepartment = new TreeItem<>("Chemistry Department");
                  TreeItem<String> mathsDepartment = new TreeItem<>("Maths Department");

                  /* Add items to root item */
                  rootItem.getChildren().addAll(physicsDepartment, chemistryDepartment, mathsDepartment);

                  /* Adding Physics faculty details to physics department */
                  TreeItem<String> physicsFaculty1 = new TreeItem<>("Srinivasa Rao");
                  TreeItem<String> physicsFaculty2 = new TreeItem<>("Madavi Latha");
                  physicsDepartment.getChildren().addAll(physicsFaculty1, physicsFaculty2);
                  physicsDepartment.setExpanded(true);

                  /* Adding chemistry faculty details to physics department */
                  TreeItem<String> chemistryFaculty1 = new TreeItem<>("Naga Veni");
                  TreeItem<String> chemistryFaculty2 = new TreeItem<>("Raghava Rao");
           chemistryDepartment.getChildren().addAll(chemistryFaculty1, chemistryFaculty2);
                  chemistryDepartment.setExpanded(true);

                  /* Adding maths faculty details to maths department */
                  TreeItem<String> mathsFaculty1 = new TreeItem<>("Venkata Anjaneyulu");
                  TreeItem<String> mathsFaculty2 = new TreeItem<>("Rama Krishna");
                  mathsDepartment.getChildren().addAll(mathsFaculty1, mathsFaculty2);
                  mathsDepartment.setExpanded(true);

Step 2: Create TreeTableView widget by passing rootItem to the TreeTableView constructor.
TreeTableView<String> treeTableView = new TreeTableView<>(rootItem);

Step 3: Create TreeTableColumn widget and add it to treeTableView.

                  TreeTableColumn<String, String> column = new TreeTableColumn<>("Column");
                  column.setPrefWidth(250);
                  column.setCellValueFactory((CellDataFeatures<String, String> p) -> new ReadOnlyStringWrapper(p.getValue().getValue()));
                 
                  treeTableView.getColumns().add(column);

Find the below working application.

TreeTableViewApp.java
package com.sample.demos;

import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableColumn.CellDataFeatures;
import javafx.scene.control.TreeTableView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class TreeTableViewApp extends Application {

 @SuppressWarnings("unchecked")
 @Override
 public void start(Stage primaryStage) throws Exception {

  /* Define Root Item */
  TreeItem<String> rootItem = new TreeItem<>("College");
  rootItem.setExpanded(true);

  /* Defile 1st level child items */
  TreeItem<String> physicsDepartment = new TreeItem<>("Physics Department");
  TreeItem<String> chemistryDepartment = new TreeItem<>("Chemistry Department");
  TreeItem<String> mathsDepartment = new TreeItem<>("Maths Department");

  /* Add items to root item */
  rootItem.getChildren().addAll(physicsDepartment, chemistryDepartment, mathsDepartment);

  /* Adding Physics faculty details to physics department */
  TreeItem<String> physicsFaculty1 = new TreeItem<>("Srinivasa Rao");
  TreeItem<String> physicsFaculty2 = new TreeItem<>("Madavi Latha");
  physicsDepartment.getChildren().addAll(physicsFaculty1, physicsFaculty2);
  physicsDepartment.setExpanded(true);

  /* Adding chemistry faculty details to physics department */
  TreeItem<String> chemistryFaculty1 = new TreeItem<>("Naga Veni");
  TreeItem<String> chemistryFaculty2 = new TreeItem<>("Raghava Rao");
  chemistryDepartment.getChildren().addAll(chemistryFaculty1, chemistryFaculty2);
  chemistryDepartment.setExpanded(true);

  /* Adding maths faculty details to maths department */
  TreeItem<String> mathsFaculty1 = new TreeItem<>("Venkata Anjaneyulu");
  TreeItem<String> mathsFaculty2 = new TreeItem<>("Rama Krishna");
  mathsDepartment.getChildren().addAll(mathsFaculty1, mathsFaculty2);
  mathsDepartment.setExpanded(true);
  
  TreeTableView<String> treeTableView = new TreeTableView<>(rootItem);

  TreeTableColumn<String, String> column = new TreeTableColumn<>("Column");
  column.setPrefWidth(250);
  column.setCellValueFactory((CellDataFeatures<String, String> p) -> new ReadOnlyStringWrapper(p.getValue().getValue()));
  
  treeTableView.getColumns().add(column);
  treeTableView.setPrefWidth(250);
  treeTableView.setShowRoot(true);

  StackPane stackPane = new StackPane();
  stackPane.getChildren().add(treeTableView);

  primaryStage.setScene(new Scene(stackPane));
  primaryStage.setTitle("Tree Table View Example");
  primaryStage.setWidth(900);
  primaryStage.setHeight(500);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

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






Previous                                                 Next                                                 Home

No comments:

Post a Comment