在TreeTableView,JavaFX中查找具有相同名称的行和列的交集

时间:2015-03-18 22:42:54

标签: javafx treetableview

考虑到我有一个treetable,其中一些行与某些列具有相同的名称。我怎样才能找出那些细胞并为它们设定一些价值。代码bellow在所有单元格中写入no,但由于某些行名称(myItem)等于列名称(Label),我希望在某些单元格中看到是。 让我们说第3列具有名称Org和第2行中具有相同名称的Item中的Item。所以我希望在单元格2 X 3上看到“是”这个词。 首先,我创建了第一列,这是一棵树。然后我插入所有其他列。

private TreeTableView<String> drawTable() {

    root.setExpanded(true);

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


    // ////////////////treetable////////////////////////////
    /**
     * makes treeTable and set the nodes to it.
     * */

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

    // ////////////////First column/////////////////////////
    /**
     * creates first column with no caption and sets treeview for that.
     * */
    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());
                }
            });
    TreeMap<String, List<TreeItem<String>>> parentChild = new TreeMap<>();
    for (TreeItem node : root.getChildren()) {
        parentChild.put(node.getValue().toString(), node.getChildren());
    }

    // //////////////////Rest Columns////////////////////////
    /**
     * go through all organizations which have a function and make column
     */
    for (Entry<String, String> ent : dc.getSortedAssignedOrg().entrySet()) {

        TreeTableColumn<String, ArrayList<String>> col = new TreeTableColumn<>();
        Label label = new Label(ent.getValue());
        col.setGraphic(label);

        TreeMap<String, List<String>> temp = (TreeMap<String, List<String>>) dc
                .getFuncTypeOrg().clone();                          


            List<String> list = temp.firstEntry().getValue();
            String key = temp.firstEntry().getKey();


                    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) {
                                    for (Entry<String, List<TreeItem<String>>> entp : parentChild
                                            .entrySet()) {
                                        for (TreeItem myItem : entp.getValue()) {

                                    if ((myItem.getValue().equals(label.getText()) {
                                        setText("yes");
                                    }
                                    else setText("No");

                                }
                                    }
                                }



                            };
                        }

                    });


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

    treeTable.setPrefWidth(1200);
    treeTable.setPrefHeight(500);
    treeTable.setShowRoot(false);
    treeTable.setEditable(false);

    treeTable.setTableMenuButtonVisible(true);

    return treeTable;
    }



    private TreeItem<String> createTreeItem(TreeMap<String, List<String>> data,
        String rootKey) {
    TreeItem<String> item = new TreeItem<>();
    item.setValue(rootKey);
    item.setExpanded(true);

    List<String> childData = data.get(rootKey);
    if (childData != null) {
        childData.stream().map(child -> createTreeItem(data, child))
                .collect(Collectors.toCollection(item::getChildren));
    }
    String valueName = item.getValue();
    item.setValue((dc.getSortedfuncAll().get(valueName)));
    return item;
}

}

1 个答案:

答案 0 :(得分:1)

我使用this.getTreeTableRow()。getItem()获取每个单元格的行名称,并且可以成功地将其与列名称进行比较。这是细胞工厂:

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>>() {
                        private List<Double> result = new ArrayList<Double>();
                        private List<Double> weight = new ArrayList<>();
                        private double lastresult = new Double(0);

                        public void updateItem(ArrayList<String> item,
                                boolean empty) {

                            super.updateItem(item, empty);



                                if (label.getText().equals(
                                        this.getTreeTableRow().getItem())
                                         {
                                    setText("yes");
                                }
                            }

                        }

                    };
                };

            });// end cell factory