setting the value of parent according to its children for each column in TreeTableView JavaFX

自作多情 提交于 2019-12-08 09:35:24

问题


The function bellow creates a treetableview, which has this structure: As you see for example there is no value for parents with child. I need to add the value of children (their weight) together and set the text of related parent to this value for each column in which the children are set. which means I want to have instead of x1, 2 and instead of x3, 1 and for x2, the value 3. Do I have to calculate the treetableview 2 times?

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

I think i should go through table here, and set cell factory, but i do not no how. I have tried to do

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

                                }
                            };
                        };

                    });

But I get this error:

the method setCellFactory(Callback,TreeTableCell>) in the type TreeTableColumn is not applicable for the arguments (new Callback,TreeTableCell>>(){})

}


回答1:


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


来源:https://stackoverflow.com/questions/28963660/setting-the-value-of-parent-according-to-its-children-for-each-column-in-treetab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!