【问题标题】:JavaFx. Change TreeItem Style by conditionJavaFX。按条件更改 TreeItem 样式
【发布时间】:2016-10-04 11:23:41
【问题描述】:

还有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();

}

每个元素都有自己的.styleProperty(),并从ViewStyle.getStyle()中获取值

此属性绑定到每个 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);
                    }
                };
            }
        });

问题是树形单元格在选择中显示得很丑。即所选单元格的颜色不会改变。只改变字母的颜色(按照默认主题),但不是很方便。因此,可能需要附加 .css 文件。同时,我不明白如何根据当前的 ViewStyle 更改单元格的样式(默认和选中时)。

【问题讨论】:

    标签: java css javafx


    【解决方案1】:

    您可以简单地将 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;");
    

    还请注意,您在updateItem 方法的覆盖版本中做了一些不应该做的事情:并不总是调用super.updateItem。这可能导致filled/empty 伪类未正确分配,并且TreeCellitem 属性不包含来自最新updateItem 调用的项目。你应该这样做:

    @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);
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-11
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      • 2017-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多