2017-03-03 67 views
0

我是Java 8世界的新手(使用lambdas,函數等)並構建JavaFX 8應用程序,我正努力爲TreeTableView構建數據模型(TreeItem)。數據從ObservableList<Certificate>從數據庫中獲取。具有ca=true的證書對象可能有可通過issuerName字段跟蹤的子項。我的目標是建立一個TreeTableView與各列,其中證書被呈現爲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; 
    ... 

在下一步要對這個觀點有些添加/刪除/編輯功能。

任何人都可以指導我如何做到這一點?

回答

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", "[email protected]"), 
       new Employee("Emma Jones", "[email protected]"), new Employee("Michael Brown", "[email protected]"), 
       new Employee("Anna Black", "[email protected]"), new Employee("Rodger York", "[email protected]"), 
       new Employee("Susan Collins", "[email protected]")); 

     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); 
      } 
     } 

    }