简体   繁体   中英

JavaFx. Change TreeItem Style by condition

There TreeView, each element of which implements 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 element has its own .styleProperty(), and get value from ViewStyle.getStyle()

This property bind for each TreeCell.styleProperty():

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);
                            return;
                        }
                        if (item != null) {
                            styleProperty().bind(item.styleProperty());
                            textProperty().bind(item.titleProperty());
                        }
                        super.updateItem(item, empty);
                    }
                };
            }
        });

The problem is that tree cells are displayed ugly in the selection. That is the color of the selected cell does not change. Changing only the color of the letters (in accordance with the default theme), but it is not very convenient. Therefore, probably it is necessary to attach .css files. At the same time, I don't understand how to change the style (default and when selected) of the cell depending on the current ViewStyle.

You could simply change the css property to one that is only used for unselected cells ( -fx-control-inner-background ):

enum ViewStyle {

    NEW("-fx-control-inner-background: b8faa7;"),
    NEW_PARENT("-fx-control-inner-background: b8ebbb;"),
    LOCKED("-fx-control-inner-background: adadad; "),
    HAS_NO_DATA("-fx-control-inner-background: eb8d8d;");

Also note that you did something you shouldn't do in a overwritten version of the updateItem method: Not always call super.updateItem . This can lead to the filled / empty pseudoclasses not being assigned correctly and the item property of the TreeCell not containing the item from the latest updateItem call. You should do something like this instead:

@Override
protected void updateItem(Viewable item, boolean empty) {
    textProperty().unbind();
    styleProperty().unbind();
    if (empty || item == null) {
        setText(null);
        setStyle(null);
    } else {
        styleProperty().bind(item.styleProperty());
        textProperty().bind(item.titleProperty());
    }
    super.updateItem(item, empty);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM