JavaFX:使用css为单个树元素着色

JavaFX:使用css为单个树元素着色,css,javafx,treeview,Css,Javafx,Treeview,我希望能够根据某些条件为treeView的单个树项目着色。 这个答案似乎不错,但我无法实施。 我无法理解如何使用setCellFactory方法格式化单个TreeItems 我有一节课 public class Bag { public String caption,assignment=""; Boolean eval; public Set<Vertex> Nodes = new HashSet<Vertex>(); public V

我希望能够根据某些条件为treeView的单个树项目着色。 这个答案似乎不错,但我无法实施。

我无法理解如何使用
setCellFactory
方法格式化单个
TreeItem
s

我有一节课

public class Bag {
    public String caption,assignment="";
    Boolean eval;
    public Set<Vertex> Nodes = new HashSet<Vertex>();
    public Vector<Bag> ChildBags = new Vector<Bag>();

     @Override
    public String toString()
    {
        return assignment+ " " +caption;
    }

}
因此,我想将eval属性为true的所有节点的标题(toString()方法返回)涂成绿色。
toString()
方法为所有节点返回的赋值字符串应为蓝色

我怎样才能做到


谢谢

通过覆盖
TreeCell
updateItem
方法,您可以根据单元格包含的
TreeCell
的值调整
TreeCell
的属性

在下面的示例中,一个伪类被分配给所有包含前缀为
“child”
的值的单元格,所有空单元格都有黑色背景

TreeView<String> treeView = ...

PseudoClass childPseudoClass = PseudoClass.getPseudoClass("child");

treeView.setCellFactory(tv -> new TreeCell<String>() {

    @Override
    protected void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        if (empty || item == null) {
            // update for empty cell / cell containing null
            pseudoClassStateChanged(childPseudoClass, false);
            setText("");
            setStyle("-fx-background-color: black;");
        } else {
            // update for filled cell
            pseudoClassStateChanged(childPseudoClass, item.startsWith("child"));
            setText(item);
            setStyle(null);
        }
    }

});

每当值更改时,
TreeView
就会调用
updateItem
方法,例如,如果新的
TreeItem
与单元格关联,或者
TreeItem
value
属性被修改

您还可以在返回前使用工厂将侦听器添加到
树单元格
,以防您喜欢这样做,例如,希望根据


编辑:要对文本应用不同的颜色,需要对文本部分使用不同的
节点

treeView.setCellFactory(tv -> new TreeCell<Bag>() {

    private final Text assignment;
    private final Text caption;
    private final Node graphic;

    {
        assignment = new Text();
        caption = new Text();
        assignment.getStyleClass().add("assignment");
        graphic = new HBox(4, assignment, caption);
        setGraphic(graphic);
    }

    @Override
    protected void updateItem(Bag item, boolean empty) {
        super.updateItem(item, empty);
        if (empty || item == null) {
            setGraphic(null);
        } else {
            setGraphic(graphic);
            assignment.setText(item.assignment);
            caption.setText(item.caption);
            caption.getStyleClass().remove("true");
            if (item.eval) {
                caption.getStyleClass().add("true");
            }
        }
    }

});
treeView.setCellFactory(tv->new TreeCell()){
私人期末作业;
私人最终文本标题;
私有最终节点图;
{
赋值=新文本();
标题=新文本();
assignment.getStyleClass().add(“assignment”);
图形=新的HBox(4,分配,标题);
设置图形(图形);
}
@凌驾
受保护的无效更新项(袋项,布尔值为空){
super.updateItem(项,空);
if(空| |项==null){
设置图形(空);
}否则{
设置图形(图形);
赋值.setText(项赋值);
caption.setText(item.caption);
caption.getStyleClass().remove(“true”);
如果(项目评估){
caption.getStyleClass().add(“true”);
}
}
}
});

要给文本上色,您需要使用
-fx fill
属性,而不是
-fx text fill
属性。

谢谢您的回答。我有一个包的树状视图。我可以为包的不同组件设置不同的样式吗(请参见编辑的问题),即赋值字符串为蓝色,而赋值为true的节点将标题设置为绿色。我不知道为什么它不起作用。请检查此处的完整代码:@HimaniVirmani:您正在使用
setStyle
标题指定颜色,但是您正在清除样式类以删除它。改用
setStyle(null)
。此外,您仍在使用
-fx text-fill
而不是
-fx-fill
,这对
text
节点不起作用。感谢它起作用。但为什么我们需要将赋值和标题的初始化放在单独的块中?为什么需要HBox?@HimaniVirmani为什么不同的
Text
元素?因为不能对同一
文本
元素使用不同的颜色。为什么是HBox
图形
只是一个
节点
,因此
文本
元素需要放置在
父节点
HBox
为此提供了合适的布局(对于多行文本,您可能需要切换到
TextFlow
)。
treeView.setCellFactory(tv -> new TreeCell<Bag>() {

    private final Text assignment;
    private final Text caption;
    private final Node graphic;

    {
        assignment = new Text();
        caption = new Text();
        assignment.getStyleClass().add("assignment");
        graphic = new HBox(4, assignment, caption);
        setGraphic(graphic);
    }

    @Override
    protected void updateItem(Bag item, boolean empty) {
        super.updateItem(item, empty);
        if (empty || item == null) {
            setGraphic(null);
        } else {
            setGraphic(graphic);
            assignment.setText(item.assignment);
            caption.setText(item.caption);
            caption.getStyleClass().remove("true");
            if (item.eval) {
                caption.getStyleClass().add("true");
            }
        }
    }

});