JavaFX TreeTableView在表列中渲染数据的层次结构。
TreeTableView组件组合了TreeView和TableView控件。

  1. import javafx.application.Application;
  2. import javafx.beans.property.ReadOnlyStringWrapper;
  3. import javafx.scene.Group;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.TreeTableColumn;
  6. import javafx.scene.control.TreeTableColumn.CellDataFeatures;
  7. import javafx.scene.control.TreeItem;
  8. import javafx.scene.control.TreeTableView;
  9. import javafx.stage.Stage;
  10. public class Main extends Application {
  11. public static void main(String[] args) {
  12. Application.launch(args);
  13. }
  14. @Override
  15. public void start(Stage stage) {
  16. final Scene scene = new Scene(new Group(), 200, 400);
  17. Group sceneRoot = (Group) scene.getRoot();
  18. TreeItem<String> childNode1 = new TreeItem<>("Node 1");
  19. TreeItem<String> childNode2 = new TreeItem<>("Node 2");
  20. TreeItem<String> childNode3 = new TreeItem<>("Node 3");
  21. TreeItem<String> root = new TreeItem<>("Root");
  22. root.setExpanded(true);
  23. root.getChildren().setAll(childNode1, childNode2, childNode3);
  24. TreeTableColumn<String, String> column = new TreeTableColumn<>("Column");
  25. column.setPrefWidth(150);
  26. column.setCellValueFactory((CellDataFeatures<String, String> p) -> new ReadOnlyStringWrapper(
  27. p.getValue().getValue()));
  28. TreeTableView<String> treeTableView = new TreeTableView<>(root);
  29. treeTableView.getColumns().add(column);
  30. sceneRoot.getChildren().add(treeTableView);
  31. stage.setScene(scene);
  32. stage.show();
  33. }
  34. }

image.png

添加几个列

  1. import java.util.Arrays;
  2. import java.util.List;
  3. import javafx.application.Application;
  4. import javafx.beans.property.ReadOnlyStringWrapper;
  5. import javafx.beans.property.SimpleStringProperty;
  6. import javafx.scene.Group;
  7. import javafx.scene.Scene;
  8. import javafx.scene.control.TreeItem;
  9. import javafx.scene.control.TreeTableColumn;
  10. import javafx.scene.control.TreeTableView;
  11. import javafx.stage.Stage;
  12. public class Main extends Application {
  13. List<Employee> employees = Arrays.<Employee> asList(new Employee(
  14. "Ethan Williams", "ethan.williams@example.com"), new Employee(
  15. "Emma Jones", "emma.jones@example.com"), new Employee("Michael Brown",
  16. "michael.brown@example.com"), new Employee("Anna Black",
  17. "anna.black@example.com"), new Employee("Rodger York",
  18. "roger.york@example.com"), new Employee("Susan Collins",
  19. "susan.collins@example.com"));
  20. final TreeItem<Employee> root = new TreeItem<>(new Employee(
  21. "Sales Department", ""));
  22. public static void main(String[] args) {
  23. launch();
  24. }
  25. @Override
  26. public void start(Stage stage) {
  27. root.setExpanded(true);
  28. employees.stream().forEach((employee) -> {
  29. root.getChildren().add(new TreeItem<>(employee));
  30. });
  31. Scene scene = new Scene(new Group(), 400, 400);
  32. Group sceneRoot = (Group) scene.getRoot();
  33. TreeTableColumn<Employee, String> empColumn = new TreeTableColumn<>(
  34. "Employee");
  35. empColumn.setPrefWidth(150);
  36. empColumn
  37. .setCellValueFactory((
  38. TreeTableColumn.CellDataFeatures<Employee, String> param) -> new ReadOnlyStringWrapper(
  39. param.getValue().getValue().getName()));
  40. TreeTableColumn<Employee, String> emailColumn = new TreeTableColumn<>(
  41. "Email");
  42. emailColumn.setPrefWidth(190);
  43. emailColumn
  44. .setCellValueFactory((
  45. TreeTableColumn.CellDataFeatures<Employee, String> param) -> new ReadOnlyStringWrapper(
  46. param.getValue().getValue().getEmail()));
  47. TreeTableView<Employee> treeTableView = new TreeTableView<>(root);
  48. treeTableView.getColumns().setAll(empColumn, emailColumn);
  49. sceneRoot.getChildren().add(treeTableView);
  50. stage.setScene(scene);
  51. stage.show();
  52. }
  53. public class Employee {
  54. private String name;
  55. private String email;
  56. public Employee() {
  57. }
  58. public Employee(String name, String email) {
  59. this.name = name;
  60. this.email = email;
  61. }
  62. public String getName() {
  63. return name;
  64. }
  65. public void setName(String name) {
  66. this.name = name;
  67. }
  68. public String getEmail() {
  69. return email;
  70. }
  71. public void setEmail(String email) {
  72. this.email = email;
  73. }
  74. }
  75. }

image.png

改变视觉外观

treeTableView.setTableMenuButtonVisible(true)启用表格菜单按钮,以便用户可以切换表列的可见性。 该方法将“+”按钮添加到表头。

  1. import java.util.Arrays;
  2. import java.util.List;
  3. import javafx.application.Application;
  4. import javafx.beans.property.ReadOnlyStringWrapper;
  5. import javafx.beans.property.SimpleStringProperty;
  6. import javafx.scene.Group;
  7. import javafx.scene.Scene;
  8. import javafx.scene.control.TreeItem;
  9. import javafx.scene.control.TreeTableColumn;
  10. import javafx.scene.control.TreeTableView;
  11. import javafx.stage.Stage;
  12. public class Main extends Application {
  13. List<Employee> employees = Arrays.<Employee> asList(new Employee(
  14. "Ethan Williams", "ethan.williams@example.com"), new Employee(
  15. "Emma Jones", "emma.jones@example.com"), new Employee("Michael Brown",
  16. "michael.brown@example.com"), new Employee("Anna Black",
  17. "anna.black@example.com"), new Employee("Rodger York",
  18. "roger.york@example.com"), new Employee("Susan Collins",
  19. "susan.collins@example.com"));
  20. final TreeItem<Employee> root = new TreeItem<>(new Employee(
  21. "Sales Department", ""));
  22. public static void main(String[] args) {
  23. Application.launch(Main.class, args);
  24. }
  25. @Override
  26. public void start(Stage stage) {
  27. root.setExpanded(true);
  28. employees.stream().forEach((employee) -> {
  29. root.getChildren().add(new TreeItem<>(employee));
  30. });
  31. Scene scene = new Scene(new Group(), 400, 400);
  32. Group sceneRoot = (Group) scene.getRoot();
  33. TreeTableColumn<Employee, String> empColumn = new TreeTableColumn<>(
  34. "Employee");
  35. empColumn.setPrefWidth(150);
  36. empColumn
  37. .setCellValueFactory((
  38. TreeTableColumn.CellDataFeatures<Employee, String> param) -> new ReadOnlyStringWrapper(
  39. param.getValue().getValue().getName()));
  40. TreeTableColumn<Employee, String> emailColumn = new TreeTableColumn<>(
  41. "Email");
  42. emailColumn.setPrefWidth(190);
  43. emailColumn
  44. .setCellValueFactory((
  45. TreeTableColumn.CellDataFeatures<Employee, String> param) -> new ReadOnlyStringWrapper(
  46. param.getValue().getValue().getEmail()));
  47. TreeTableView<Employee> treeTableView = new TreeTableView<>(root);
  48. treeTableView.getColumns().setAll(empColumn, emailColumn);
  49. treeTableView.setTableMenuButtonVisible(true);
  50. sceneRoot.getChildren().add(treeTableView);
  51. stage.setScene(scene);
  52. stage.show();
  53. }
  54. public class Employee {
  55. private SimpleStringProperty name;
  56. private SimpleStringProperty email;
  57. public SimpleStringProperty nameProperty() {
  58. if (name == null) {
  59. name = new SimpleStringProperty(this, "name");
  60. }
  61. return name;
  62. }
  63. public SimpleStringProperty emailProperty() {
  64. if (email == null) {
  65. email = new SimpleStringProperty(this, "email");
  66. }
  67. return email;
  68. }
  69. private Employee(String name, String email) {
  70. this.name = new SimpleStringProperty(name);
  71. this.email = new SimpleStringProperty(email);
  72. }
  73. public String getName() {
  74. return name.get();
  75. }
  76. public void setName(String fName) {
  77. name.set(fName);
  78. }
  79. public String getEmail() {
  80. return email.get();
  81. }
  82. public void setEmail(String fName) {
  83. email.set(fName);
  84. }
  85. }
  86. }

image.png
我们可以通过使用TreeTableView类的setShowRoot方法显示或隐藏根树项目。

  1. treeTableView.setShowRoot(false);

分类

我们可以通过点击列标题对数据进行排序。
设置列的降序排序模式

  1. aColumn.setSortType(TreeTableColumn.SortType.DESCENDING);

设置列的升序排序模式

  1. aColumn.setSortType(TreeTableColumn.SortType.ASCENDING);

将排序模式应用于所有树项目

  1. treeTableView.setSortMode(TreeSortMode.ALL_DESCENDANTS);

仅将排序模式应用于第一级节点

  1. treeTableView.setSortMode(TreeSortMode.ONLY_FIRST_LEVEL);

管理选择模式

TreeTableView类的默认选择模型是SelectionMode.SINGLE。
要启用树项目和单元格的多重选择,请使用setSelectionModel和setCellSelectionEnabled方法的组合。
启用单元格的多重选择

  1. treeTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
  2. treeeTableView.getSelectionModel().setCellSelectionEnabled(true);