考虑我有一个树表,其中某些行与某些列具有相同的名称。我怎样才能找出这些单元格并为它们设置一些值。下面的代码在所有单元格中都写不,但由于某些行名(myItem)等于列名(标签),我希望在某些单元格中看到是。假设第 3 列的名称为 Org,并且树的第 2 行中的 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;
}
}