为TreeTableView JavaFX中的每一列根据其子级设置父级的值 [英] setting the value of parent according to its children for each column in TreeTableView JavaFX

查看:94
本文介绍了为TreeTableView JavaFX中的每一列根据其子级设置父级的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下函数将创建一个treetableview,其结构如下: 如您所见,带孩子的父母没有任何价值.我需要将子项的值(它们的权重)加在一起,并在其中设置子项的每一列中将相关父项的文本设置为此值.这意味着我想要代替x1、2,而不是x3、1,对于x2,它的值3.我必须计算2次treetableview吗?

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

                                }
                            };
                        };

                    });

但是我得到这个错误:

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

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

}

推荐答案

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

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

这篇关于为TreeTableView JavaFX中的每一列根据其子级设置父级的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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