0

我已经创建了这个树表 树表

现在我想总结子值并将结果显示在相关列下的父单元格中。例如,对于第 2 列和第 2 行中的函数 7,我想要右 2.0,而对于函数 11 第 4 行第 4 行右 1.0(函数 12 + 函数 13)

这是生成树表的代码。

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

    // Getting Keys that have children//////
    Set<String> keyFromcombined = new HashSet<>();
    List<String> valueOfCombined = new ArrayList<String>();
    for (Entry<String, List<String>> ent : dc.getCombiFunc().entrySet()) {
        for (int i = 0; i < ent.getValue().size(); i++)
            valueOfCombined.add(ent.getValue().get(i));
    }
    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.setPrefWidth(50);
    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();

                    if (list.get(1).equals(param.getValue().getValue())
                            && !list.get(5).equals(label.getText())) {
                        result.add("white");
                    }
                    if (!root.isLeaf()) {

                        result.add("parent");
                    }
                    if (list.get(1).equals(param.getValue().getValue())
                            && list.get(5).equals(label.getText())) {
                        result.add(0, list.get(2));// weight

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

                            // setText("DD");
                        } else if (item.contains("parent")) {
                            for (int i = 0; i < dc.getFuncTypeOrg().size(); i++) {

                            }

                            String text = param.getCellData(root).get(0);
                            // setText(text);
                        }
                    }
                };
            };

        });// end cell factory


        treeTable.getColumns().add(col);
    }//end for loop col

TreeMap 临时克隆 dc.getFuncTypeOrg()。在这个 TreeMap 中,我对每个孩子都有价值(颜色和数字)。然后在 cellfactory 中,我将值乘以颜色(绿色 = 1 和黄色 = 0)。在循环之外,我想制作一个包含每个父级作为键和所有子级作为值的树形图。然后我可以将子值汇总在一起并制作一个树形图,其中第一个键是父键,值是所需的值(或只是字符串 ArrayList )。之后,我可以检查 cellFactory 中单元格的名称,如果它是父级,则正好是单元格中的值。有人告诉我如何获得 treeitem 值,我现在在这里:

//after col loop ends
TreeMap<String, List<TreeItem<String>>> mytreemap = new TreeMap<>(); 
    TreeMap<String, List<String>> parChild = new TreeMap<>();

    for(TreeItem node: root.getChildren()){
        if(!node.isLeaf())
            mytreemap.put(node.getValue().toString(), node.getChildren());
        }

        for(Entry<String, List<TreeItem<String>>> ent: mytreemap.entrySet()){

            for(TreeItem myItem : ent.getValue()){
                // how can i fill parChild with parent as key and all its children as value?
                System.out.println(ent.getKey()+"  "+myItem.getValue());
            }
        }

    treeTable.setPrefWidth(1200);
    treeTable.setPrefHeight(500);
    treeTable.setShowRoot(false);
    treeTable.setTableMenuButtonVisible(true);
    return treeTable; }

在这里 setCellFactory

     else if (item.contains("parent")) {
                            for (int i = 0; i < dc.getFuncTypeOrg().size(); i++) {

                            }

我可以找到根源。有没有办法进行递归(最多为该根单元格的子项和子项的数量)并将它们的值加在一起并将父单元格设置为该值?

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:02:28.783 回答