简体   繁体   English

折叠/展开treeitem会更改JavaFX中的单元格值

[英]collapsing/ expanding treeitem changes cell values, in JavaFX

I have a treetable in which I setText for a cell when the name of it's row is equal to the name of it's column. 我有一个树形表,当它的行名等于它的列名时,我在其中为一个单元格设置了setText。 It works successfully but, when I collapse or extend a treeitem or scroll down the value changes it's cell. 它可以成功工作,但是当我折叠或扩展树项目或向下滚动时,值更改为它的单元格。 How can I make overcome this problem? 我该如何克服这个问题? Do I have to setText for a cell at updateItem method? 我必须在updateItem方法中为单元格设置setText吗?

private TreeTableView<String> drawTable() {

    root.setExpanded(true);


    // ////////////////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.setEditable(false);
    firstColumn.setSortable(false);
    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////////////////////////
    /**
     * 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);
        col.setEditable(false);
        col.setSortable(false);


            // //////////////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 (this.getTreeTableRow().getItem().equals(
                                  label.getText()) {
                                    setText("yes");
                                }
                            }

                        }

                    };
                };

            });// end cell factory

        treeTable.getColumns().add(col);
    }
    // end for col

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

    treeTable.setTableMenuButtonVisible(true);

    return treeTable;
}

//makes the tree hierarchy///
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;
}

 }

尝试使用以下内容代替setText。

this.getTreeTableRow().setItem("yes");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM