Monday 18 June 2018

JavaFX: TreeTableView: Adding multiple columns

In this post, I am going to show how to add multiple columns to tree table.

I am going to add all my friends details to TreeTableView widget. Below step-by-step procedure explains how can we achieve this.

Step 1: Create a model class Friend.

public class Friend {
         private String firstName;
         private String lastName;
         private String email;
         ....
         ....
}

Step 2: Create tree root item widget.
TreeItem<Friend> rootItem = new TreeItem<>(new Friend("All My Friend list", "", ""));
rootItem.setExpanded(true);

Step 3: Create list of friends and add them to root item.
List<Friend> friends = Arrays.asList(new Friend("Hari", "Krishna", "krishna@krishna.com"),new Friend("Bhairava", "beddu", "bhairava@bhairava.com"),new Friend("ahswani", "sharmaSekar", "ashwani@ashwani.com"),new Friend("chandra", "babu", "chandra@babu.com"));

friends.stream().forEach((friend) -> {
         rootItem.getChildren().add(new TreeItem<>(friend));
});

Step 4: Define TreeTableView widget
TreeTableView<Friend> treeTableView = new TreeTableView<>(rootItem);

Step 5: Add columns to tree table view widget.
                  TreeTableColumn<Friend, String> firstNameColumn = new TreeTableColumn<>("First Name");
                  firstNameColumn.setPrefWidth(150);
                  firstNameColumn.setCellValueFactory(
                                    (TreeTableColumn.CellDataFeatures<Friend, String> param) -> new ReadOnlyStringWrapper(
                                                      param.getValue().getValue().getFirstName()));

                  TreeTableColumn<Friend, String> lastNameColumn = new TreeTableColumn<>("Last Name");
                  lastNameColumn.setPrefWidth(150);
                  lastNameColumn.setCellValueFactory(
                                    (TreeTableColumn.CellDataFeatures<Friend, String> param) -> new ReadOnlyStringWrapper(
                                                      param.getValue().getValue().getLastName()));

                  TreeTableColumn<Friend, String> emailColumn = new TreeTableColumn<>("Email");
                  emailColumn.setPrefWidth(150);
                  emailColumn.setCellValueFactory(
                                    (TreeTableColumn.CellDataFeatures<Friend, String> param) -> new ReadOnlyStringWrapper(
                                                      param.getValue().getValue().getEmail()));

                  treeTableView.getColumns().addAll(firstNameColumn, lastNameColumn, emailColumn);

Find the below working application.

Friend.java
package com.sample.model;

public class Friend {
 private String firstName;
 private String lastName;
 private String email;

 public Friend(String fName, String lName, String email) {
  this.firstName = fName;
  this.lastName = lName;
  this.email = email;
 }

 public String getFirstName() {
  return firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public String getEmail() {
  return email;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public void setEmail(String email) {
  this.email = email;
 }

}

TreeTableViewApp.java
package com.sample.demos;

import java.util.Arrays;
import java.util.List;

import com.sample.model.Friend;

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.TreeTableView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class TreeTableViewApp extends Application {

 List<Friend> friends = Arrays.asList(new Friend("Hari", "Krishna", "krishna@krishna.com"),
   new Friend("Bhairava", "beddu", "bhairava@bhairava.com"),
   new Friend("ahswani", "sharmaSekar", "ashwani@ashwani.com"),
   new Friend("chandra", "babu", "chandra@babu.com"));

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

  /* Define Root Item */
  TreeItem<Friend> rootItem = new TreeItem<>(new Friend("All My Friend list", "", ""));
  rootItem.setExpanded(true);

  friends.stream().forEach((friend) -> {
   rootItem.getChildren().add(new TreeItem<>(friend));
  });

  TreeTableView<Friend> treeTableView = new TreeTableView<>(rootItem);

  TreeTableColumn<Friend, String> firstNameColumn = new TreeTableColumn<>("First Name");
  firstNameColumn.setPrefWidth(150);
  firstNameColumn.setCellValueFactory(
    (TreeTableColumn.CellDataFeatures<Friend, String> param) -> new ReadOnlyStringWrapper(
      param.getValue().getValue().getFirstName()));

  TreeTableColumn<Friend, String> lastNameColumn = new TreeTableColumn<>("Last Name");
  lastNameColumn.setPrefWidth(150);
  lastNameColumn.setCellValueFactory(
    (TreeTableColumn.CellDataFeatures<Friend, String> param) -> new ReadOnlyStringWrapper(
      param.getValue().getValue().getLastName()));

  TreeTableColumn<Friend, String> emailColumn = new TreeTableColumn<>("Email");
  emailColumn.setPrefWidth(150);
  emailColumn.setCellValueFactory(
    (TreeTableColumn.CellDataFeatures<Friend, String> param) -> new ReadOnlyStringWrapper(
      param.getValue().getValue().getEmail()));

  treeTableView.getColumns().addAll(firstNameColumn, lastNameColumn, emailColumn);
  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