Tuesday, 19 June 2018

TreeTableView: Enable cell selection

By enabling the cell selection mode, you can select individual cell.

Ex
treeTableView.getSelectionModel().setCellSelectionEnabled(true);



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(false);
  treeTableView.setTableMenuButtonVisible(false);
  treeTableView.getSelectionModel().setCellSelectionEnabled(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