0

下面的函数创建了一个树表视图,它具有以下结构:例如,正如您所看到的,对于有孩子的父母来说没有价值。我需要将子项的值(他们的体重)加在一起,并将相关父项的文本设置为设置子项的每一列的该值。这意味着我想要而不是 x1, 2 而不是 x3, 1 和 x2 的值 3。我必须计算 treetableview 2 次吗?

          1   2    3    4
- a       x1       x3
  - a1    1        1
  - a2    1        0
- b          x2
  - b1        3
- c       1   1

private void drawTable() {
    root.setExpanded(true);
    Set<String> combinedKeys = new HashSet<>(dc.getCombiFunc().keySet());
    Set<String> funcAllKeys = new HashSet<>(dc.getSortedfuncAll().keySet());
    funcAllKeys.removeAll(dc.getCombiFunc().keySet());
    for (List<String> value : dc.getCombiFunc().values()) {
        funcAllKeys.removeAll(value);
    }
    for (String valueremained : funcAllKeys) {
        ArrayList<String> tempNameId = new ArrayList<>();
        tempNameId.add(dc.getSortedfuncAll().get(valueremained));
        // all elements which are not in combined functions (They are all
        // orphan)
        root.getChildren().add(new TreeItem<String>(tempNameId.get(0)));
    }
    Set<String> keyFromcombined = new HashSet<>();
    List<String> valueOfCombined = new ArrayList<String>();
    for (Entry<String, List<String>> ent : dc.getCombiFunc().entrySet()) {
        valueOfCombined.add(ent.getValue().get(0));
    }
    List<String> rootKeyList = new ArrayList<>();
    for (String key : combinedKeys) {

        if (!valueOfCombined.contains((key))) {

            keyFromcombined.add(dc.getFuncAll().get(key));
            rootKeyList.add(key);
        }
    }
    String[] rootKeys = rootKeyList.toArray(new String[rootKeyList.size()]);

    // ////////////////treetable////////////////////////////

    treeTable.setRoot(root);
    Arrays.stream(rootKeys).forEach(
            rootKey -> root.getChildren().add(
                    createTreeItem(dc.getCombiFunc(), rootKey)));

    // ////////////////First column/////////////////////////

    TreeTableColumn<String, String> firstColumn = new TreeTableColumn<>("");
    treeTable.getColumns().add(firstColumn);// Tree column
    firstColumn
            .setCellValueFactory(new Callback<CellDataFeatures<String, String>, ObservableValue<String>>() {
                public ObservableValue<String> call(
                        CellDataFeatures<String, String> p) {
                    return new ReadOnlyStringWrapper(p.getValue()
                            .getValue());
                }
            });

    // //////////////////Rest Columns////////////////////////

    for (Entry<String, String> ent : dc.getSortedAssignedOrg().entrySet()) {

        TreeTableColumn<String, ArrayList<String>> col = new TreeTableColumn<>();
        Label label = new Label(ent.getValue());
        col.setGraphic(label);
        label.setTooltip(new Tooltip(label.getText()));// tooltip for column
                                                        // headers
        col.setPrefWidth(45);
        // cell Value Factory////////////////////////
        col.setCellValueFactory(new Callback<TreeTableColumn.CellDataFeatures<String, ArrayList<String>>, ObservableValue<ArrayList<String>>>() {
            @Override
            public ObservableValue<ArrayList<String>> call(
                    CellDataFeatures<String, ArrayList<String>> param) {
                TreeMap<String, List<String>> temp = (TreeMap<String, List<String>>) dc
                        .getFuncTypeOrg().clone();
                ArrayList<String> result = new ArrayList<>();
                for (int i = 0; i < dc.getFuncTypeOrg().size(); i++) {
                    List<String> list = temp.firstEntry().getValue();

                    String key = temp.firstEntry().getKey();
                    // root.getChildren();
                    if (!param.getValue().getValue().contains("Functions")) {
                        if (param.getValue().isLeaf())
                            System.out.println(param.getValue()
                                    .getChildren()
                                    + " "
                                    + param.getValue().getParent());
                    }
                    if (list.get(1).equals(param.getValue().getValue())
                            && list.get(5).equals(label.getText())) {
                        result.add(0, list.get(2));// weight

                        // TreeItem<String> tempNodes = null;
                        // if(!param.getValue().getValue().equals("Functions")){
                        // tempNodes = param.getValue()
                        // .getParent();
                        // System.out.println(tempNodes);
                        // }

                        if (list.size() > 6) {
                            result.add(1, list.get(list.size() - 1));// color
                            result.add(2, list.get(6));// App component
                        }

                        else
                        result.add("white");
                        result.add("noOrg");

                    } else {
                        temp.remove(key);
                    }

                }

                return new ReadOnlyObjectWrapper<ArrayList<String>>(result);
            }
        }); // end cell Value Factory

        // //////////////cellfactory/////////////////////////
        col.setCellFactory(new Callback<TreeTableColumn<String, ArrayList<String>>, TreeTableCell<String, ArrayList<String>>>() {
            @Override
            public TreeTableCell<String, ArrayList<String>> call(
                    TreeTableColumn<String, ArrayList<String>> param) {
                return new TreeTableCell<String, ArrayList<String>>() {
                    public void updateItem(ArrayList<String> item,
                            boolean empty) {
                        super.updateItem(item, empty);

                        if (item == null || empty) {
                            setStyle("");
                            setText("");
                        } else if (item.contains("Green")) {
                            float weightInt = Float.parseFloat(item.get(0));
                            float res = weightInt * 1;
                            String resString = Float.toString(res);
                            this.setStyle("-fx-background-color:green");
                            setTooltip(new Tooltip(item.get(2)));
                            setText(resString);
                        } else if (item.contains("yellow")) {
                            this.setStyle("-fx-background-color:yellow");
                            setTooltip(new Tooltip(item.get(2)));
                            setText("0");
                        } else if (item.contains("white")) {
                            this.setStyle("-fx-background-color:linear-gradient(black, white, black, white); ");

                            setText("DD");
                        }
                    }
                };
            };

        });// end cell factory

        treeTable.getColumns().add(col);
    }

我想我应该在这里通过表格,并设置单元工厂,但我不知道如何。我试过做

for(  TreeTableColumn<String, ?> tt : treeTable.getColumns()){
    for(TreeItem node : root.getChildren()){
        if(!node.isLeaf()){

            tt.setCellFactory(
                    new Callback<TreeTableColumn<String, ?>, TreeTableCell<String, ArrayList<String>>>(){

                        @Override
                        public TreeTableCell<String, ArrayList<String>> call(
                                TreeTableColumn<String,?> param) {
                            return new TreeTableCell<String, ArrayList<String>>() {
                                public void updateItem(ArrayList<String> item,
                                        boolean empty) {
                                    super.updateItem(item, empty);

                                }
                            };
                        };

                    });

但我得到这个错误:

TreeTableColumn 类型中的方法 setCellFactory(Callback,TreeTableCell>) 不适用于参数 (new Callback,TreeTableCell>>(){})

}

4

1 回答 1

1

您可以使用 onEditCommit 方法添加所有子值并将它们显示在父单元格中。例如

column1.setOnEditCommit((evt) -> {
            //finalsing value of the cell
            evt.getRowValue().getValue().setCellValue((evt.getNewValue()));
            //Returns all the sibblings of the current cell
            ObservableList<TreeItem> children = evt.getRowValue().getParent().getChildren();
            int parentValue = 0;
            for (TreeItem<> child : children) {
                parentValue = parentValue + Integer.valueOf(child.getValue().getCellValue());
            }
            evt.getRowValue().getParent().getValue().setCellValue(parentValue);
        });
于 2015-10-14T08:05:35.813 回答