添加childern值并将其写入父级JavaFX中 [英] Add up childern values and write them in parent, JavaFX

查看:120
本文介绍了添加childern值并将其写入父级JavaFX中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了这个treetable

I have creating this treetable

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

Now I want to sum up th children values and show the result in the parent cell under the related column. For example for Function 7 in column 2 and row 2 I want to right 2.0, and for Function 11 column 4 row 4 right 1.0 (function 12 + function 13)

这里是产生treetable的代码。

Here is the code which produces the treetable.

    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值,我现在在这里:

TreeMap temp clones dc.getFuncTypeOrg(). In this TreeMap I have value for each child (color and the number). then in cellfactory i multiply value in color ( green = 1 and yellow = 0). Outside the loop I thought to make a treemap containg each parent as key and all it's children as value. Then I can sum up children values together and make a treemap in which first key is parent and as value the required value(or just string ArrayList ). After that I can check the name of cell in cellFactory and if it is a parent just right the value in the cell. I have been told how i can get treeitem values, and i am now here :

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

此处at setCellFactory

Here at setCellFactory

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

                            }

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

i can get the roots. Is there a way to do a recursion (up to the number of children and subchildren for that root cell) and add their value together and setText the parent cell to that value?

推荐答案

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

You can use onEditCommit method to add all childern values and show them in parent cell. For example

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

这篇关于添加childern值并将其写入父级JavaFX中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆