TreeTableView数据模型

时间:2017-03-03 09:23:49

标签: java-8 javafx-2 javafx-8

我是Java 8世界的新手(使用lambdas,函数等)并构建JavaFX 8应用程序,我正在努力为TreeTableView构建数据模型(TreeItem)。数据从ObservableList<Certificate>作为数据库提取。具有ca=true的证书对象可以生成可由issuerName字段跟踪的子项。我的目标是构建一个包含各种列的TreeTableView,其中证书显示为

Root(a dummy node)
|
|--Certificate1 (could be ca=false with no issuer match to any ca OR ca=true with no child)
|--Certificate2
|--Certificate3 (ca=true)
    |--Certifciate4 (issuer name machted with Certificate3)
    |--Certificate5 (issuer name machted with Certificate3)
|--Certificate6
|--Certificate7 (ca=true)
    |--Certifciate8 (issuer name machted with Certificate7)
    |--Certificate9 (issuer name machted with Certificate7)

班级Certificate看起来像

public class Certificate implements Serializable {
    private static final long serialVersionUID = 1L;

    private int id;
    private String name;
    private boolean ca;
    private String issuerName;
    ...

在下一步中,需要在此视图上添加/删除/编辑功能。

任何人都可以指导我如何实现这一目标

1 个答案:

答案 0 :(得分:0)

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeTableView;
import javafx.stage.Stage;

public class TreeTableSampleApp extends Application {

@Override public void start(Stage primaryStage) {
    primaryStage.setTitle("TreeTable View Sample");
    primaryStage.setScene(new Scene(new TreeTableSample()));
    primaryStage.sizeToScene();
    primaryStage.show();
    TreeTableView<?> treeTableView = (TreeTableView<?>) primaryStage.getScene().getRoot().lookup(".tree-table-view");
    treeTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
}

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


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

import ensemble.Sample;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.scene.layout.Pane;

    public class TreeTableSample extends Pane {

        List<Employee> employees = Arrays.<Employee> asList(new Employee("Ethan Williams", "ethan.williams@example.com"),
                new Employee("Emma Jones", "emma.jones@example.com"), new Employee("Michael Brown", "michael.brown@example.com"),
                new Employee("Anna Black", "anna.black@example.com"), new Employee("Rodger York", "roger.york@example.com"),
                new Employee("Susan Collins", "susan.collins@example.com"));

        final TreeItem<Employee> root = new TreeItem<>(new Employee("Sales Department", ""));

        public TreeTableSample() {
            root.setExpanded(true);
            employees.stream().forEach((employee) -> {
                root.getChildren().add(new TreeItem<>(employee));
            });

            TreeTableColumn<Employee, String> empColumn = new TreeTableColumn<>("Employee");
            empColumn.setPrefWidth(150);
            empColumn.setCellValueFactory((TreeTableColumn.CellDataFeatures<Employee, String> param) -> new ReadOnlyStringWrapper(
                    param.getValue().getValue().getName()));

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

            TreeTableView<Employee> treeTableView = new TreeTableView<>(root);
            treeTableView.getColumns().setAll(empColumn, emailColumn);
            getChildren().add(treeTableView);
        }

        public class Employee {

            private SimpleStringProperty name;
            private SimpleStringProperty email;

            public SimpleStringProperty nameProperty() {
                if (name == null) {
                    name = new SimpleStringProperty(this, "name");
                }
                return name;
            }

            public SimpleStringProperty emailProperty() {
                if (email == null) {
                    email = new SimpleStringProperty(this, "email");
                }
                return email;
            }

            private Employee(String name, String email) {
                this.name = new SimpleStringProperty(name);
                this.email = new SimpleStringProperty(email);
            }

            public String getName() {
                return name.get();
            }

            public void setName(String fName) {
                name.set(fName);
            }

            public String getEmail() {
                return email.get();
            }

            public void setEmail(String fName) {
                email.set(fName);
            }
        }

    }