简体   繁体   English

JavaFX。 按条件更改 TreeItem 样式

[英]JavaFx. Change TreeItem Style by condition

There TreeView, each element of which implements Viewable interface:有 TreeView,其中的每个元素都实现了 Viewable 接口:

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()每个元素都有自己的 .styleProperty(),并从 ViewStyle.getStyle() 获取值

This property bind for each TreeCell.styleProperty():每个 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.因此,可能需要附加 .css 文件。 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.同时,我不明白如何根据当前的 ViewStyle 更改单元格的样式(默认和选中时)。

You could simply change the css property to one that is only used for unselected cells ( -fx-control-inner-background ):您可以简单地将 css 属性更改为仅用于未选定单元格的属性( -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 .另请注意,您在updateItem方法的覆盖版本中做了不应该做的updateItem :并不总是调用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.这可能导致未正确分配已filled / empty伪类,并且TreeCellitem属性不包含来自最新updateItem调用的项目。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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