There is a tree, each element of which inherits the Viewable interface:

public interface Viewable { enum ViewStyle { NEW("-fx-background-color: b8faa7;"), NEW_PARENT("-fx-background-color: b8ebbb;"), LOCKED("-fx-background-color: adadad; "), HAS_NO_DATA("-fx-background-color: eb8d8d;"); String style; ViewStyle(String style){ this.style = style; } public String getStyle() { return style; } } ViewStyle getViewStyle(); void setViewStyle(ViewStyle style); StringProperty styleProperty(); String getTreeItemTitle(); void setTreeItemTitle(String title); StringProperty titleProperty(); } 

Each object has its own styleProperty() , the value takes from ViewStyle.getStyle()

In the tree itself, to each TreeCell this property is "bound":

 treeView.setCellFactory(new Callback<TreeView<Viewable>, TreeCell<Viewable>>() { @Override public TreeCell<Viewable> call(TreeView<Viewable> param) { return new TreeCell<Viewable>() { @Override protected void updateItem(Viewable item, boolean empty) { textProperty().unbind(); styleProperty().unbind(); if (empty || item == null) { setGraphic(null); textProperty().set(null); styleProperty().set(null); } if (item != null) { styleProperty().bind(item.styleProperty()); textProperty().bind(item.titleProperty()); } super.updateItem(item, empty); } }; } }); 

The problem is that the tree rows are ugly displayed when selected. Those. the color of the selected cell does not change. Understand that a cell can only be selected by changing the color of the letters, and this is not very convenient. By this, you probably have to attach .css files. It is not clear how to change the style of the cell depending on the current ViewStyle .

    1 answer 1

    If I were you, I would really make it through css and classes, not styles.

    About the following

    .css file

     .new { -fx-background-color : red; другие свойства } .newParent { } .locked { } .hasNoData { } 

    Viewable.ViewStyle

     enum ViewStyle { NEW("new"), NEW_PARENT("newParent"), LOCKED("locked"), HAS_NO_DATA("hasNoData"); String style; ViewStyle(String style) { this.style = style; } public String getStyle() { return style; } } 

    and in the factory would write the following

     @Override protected void updateItem(Viewable item, boolean empty) { textProperty().unbind(); getStyleClass().removeAll(Arrays.toList(Viewable.ViewStyle.values())); if (empty || item == null) { setGraphic(null); textProperty().set(null); return; } if (item != null) { getStyleClass().addAll(item.getViewStyle().getStyle()); textProperty().bind(item.titleProperty()); } super.updateItem(item, empty); } 
    • All the same, I am not sure of the correctness of the decision, since updateItem() is called when the tree changes. Those. ViewStyle information is processed in a parallel stream, and the values ​​of the ViewStyle change. Will this change in real time, or will it have to rebuild the tree every time? - I. Perevoz
    • True, it turns out that it will not. Then try to get started on styleClassProperty. - Andrew Bystrov
    • those. according to the principle as in the question code to styleProperty ()? - I. Perevoz
    • Absolutely right - Andrew Bystrov