[英]Colorize Sub-item, sub-sub-item, sub-sub-sub-item of TreeView
我正在制作一個顯示字符串樹視圖的程序。 當我制作 TreeView 時,有沒有辦法直接在 CSS 中根據項目的級別對字符串 itens 進行着色?
treeView.getStylesheets().add("style.css");
.tree-cell:sub-tree-item {
-fx-text-fill: red;
}
.tree-cell:sub-tree-item:sub-tree-item { /*Does exists something similar of this?? */
-fx-background-color: green ;
}
...
或者我必須制作標簽
TreeItem treeNode1 = new TreeItem("");
Label lteste11=new Label("myLevelIs1");
lteste11.setTextFill(Color.RED);
treeNode1.setGraphic(lteste11);
TreeItem treeNode2 = new TreeItem("");
Label lteste22=new Label("myLevelIs2");
lteste22.setTextFill(Color.GREEN);
treeNode2.setGraphic(lteste22);
TreeItem treeNodeROOT = new TreeItem();
treeNodeROOT.getChildren().addAll(treeNode1, treeNode2);
treeView.setRoot(treeNodeROOT);
您可以使用自定義單元工廠根據深度添加樣式類:
TreeView<String> tree = ... ;
tree.setCellFactory(tv -> new TreeCell<>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty) ;
getStyleClass().removeIf(s -> s.startsWith("depth"));
if (empty || item == null) {
setText("");
} else {
int depth = 0 ;
for (TreeItem<String> i = getTreeItem().getParent() ; i != null ; i = i.getParent()) {
depth++ ;
}
getStyleClass().add("depth"+depth);
setText(item);
}
}
});
然后在你的樣式表中做,例如:
.tree-cell.depth1 {
-fx-text-fill: red ;
}
.tree-cell.depth2 {
-fx-background-color: green ;
}
盡可能多的深度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.