-1

Consider that I have a treetable in which some rows have same name as some columns. How can I find out those cells and set some value to them. The code bellow writes in all cells no, but as some row names (myItem) are equal to column name (Label), I expect to see yes in some cells. Lets say column 3 has name Org and an Item in row 2 at tree with the same name. so I expect to see at cell 2 X 3, the word yes. At first I create the first column, which is a tree. Then I insert all other columns.

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

}
Iman
  • 769
  • 1
  • 13
  • 51
  • Your question (and code) is very unclear (to me, at least). It might be easier to create a simple [MCVE](http://stackoverflow.com/help/mcve) from scratch that shows clearly what you are trying to do. But anyway, you have `TreeItem myItem`, while `label.getText()` returns a `String`, so `myItem.equals(label.getText())` can never be `true`. – James_D Mar 18 '15 at 22:46
  • yes, you are right. I added now .getValue() to myItem. It prints out string. But still the collision point of column and row is not filled with word yes – Iman Mar 18 '15 at 22:55
  • 1
    Well, also you are repeatedly setting the cell factory (many, many times) on the same column, as far as I can tell. Each call to `setCellFactory` will replace the cell factory set on the previous call, so most of this code is redundant. But only the cell factory with the last value of `i` in the outermost loop, and the last value of `entp` in the middle loop, and the last value of `myItem` in the innermost loop will be the one used. But I really have no clue what you are trying to do here. You need to clean up the code so it is understandable. – James_D Mar 18 '15 at 23:03
  • I just want to go through all tree item names and compare them with each column name. Then if there is a match, set a text for that cell. – Iman Mar 18 '15 at 23:24

1 Answers1

0

I got the row name for each cell with this.getTreeTableRow().getItem() and could successfully compare it with column name. Here is the cell factory:

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
Iman
  • 769
  • 1
  • 13
  • 51