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

时间:2015-03-18 12:38:49

标签: java javafx treetableview

我有一个treetable,当它的名称行等于它的名字列时,我将setText作为单元格。它成功地工作但是,当我折叠或扩展树项目或向下滚动时,值会更改它的单元格。我怎样才能克服这个问题?我是否必须在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;
}

 }

1 个答案:

答案 0 :(得分:0)

尝试使用以下代替setText。

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