我有一个树表,当它的行名等于它的列名时,我在其中为一个单元格设置文本。它可以成功运行,但是当我折叠或扩展树项或向下滚动时,值会更改它的单元格。我怎样才能克服这个问题?我是否必须在 updateItem 方法中为单元格设置文本?
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;
}
}