【问题标题】:TableView Cell skips with the color [closed]TableView Cell 跳过颜色[关闭]
【发布时间】:2021-09-05 19:17:20
【问题描述】:

各个单元格的颜色不是我指定的。

每行“在线”应为白色,每行“离线”应略带绿色。这一切都有效,但只要我向下和向上滚动,“在线”上的那些就会将绿色作为背景色......

tblIP.setRowFactory(tv -> new TableRow<Device>() {
        @Override
        public void updateItem(Device item, boolean empty) {
            super.updateItem(item, empty);
            if(item == null || empty) {
                setStyle("");
                setText(null);
                this.getStyleClass().add("Offline");
                this.getStyleClass().remove("Online");
                this.getStyleClass().remove("deactive");
            } else if (!item.getActive().get()){
                this.getStyleClass().remove("Offline");
                this.getStyleClass().remove("Online");
                this.getStyleClass().add("deactive");
            } else if (item.getStatus().get() == 1) {
                this.getStyleClass().remove("Offline");
                this.getStyleClass().add("Online");
                this.getStyleClass().remove("deactive");
            } else if (item.getStatus().get() == 0){
                this.getStyleClass().add("Offline");
                this.getStyleClass().remove("Online");
                this.getStyleClass().remove("deactive");
            }
        }
    });

CSS:

.Offline {
    -fx-background-color: rgb(217,153,153);
    -fx-text-fill: black;
}

.Online {
    -fx-background-color: transparent;
    -fx-text-fill: black;
}

.deactive {
    -fx-background-color: rgb(196,215,155);
    -fx-text-fill: black;
}

所以到目前为止颜色分配工作,但是当我滚动时颜色不正确。我想我滚动得太快了,“updateItem”没有落后。

【问题讨论】:

  • 请用英文写下您的问题,Stack Overflow is an English only site.
  • minimal reproducible example please .. 那说:当滚动出现问题时,添加/删除样式的逻辑通常是错误的(fi 样式类可以添加多次,因此它们倾向于如果没有完全清洁会堆积)
  • 我该如何清理这个?问题是当我向下滚动表格时,实际的白色字段是绿色的。

标签: java javafx tableview


【解决方案1】:

好吧,我自己解决了这个问题。我只是简单地添加了 this.getStyleClass().clear();。

代码:

tblIP.setRowFactory(tv -> new TableRow<Device>() {
        @Override
        public void updateItem(Device item, boolean empty) {
            super.updateItem(item, empty);
            this.getStyleClass().removeAll("deactive", "Online", "Offline"); <- Thanks James_D
            if(item == null || empty) {
                setStyle("");
                setText(null);
            } else if (!item.getActive().get()){
                this.getStyleClass().add("deactive");
            } else if (item.getStatus().get() == 1) {
                this.getStyleClass().add("Online");
            } else if (item.getStatus().get() == 0){
                this.getStyleClass().add("Offline");
            }
        }
    });

【讨论】:

  • 使用this.getStyleClass().removeAll("deactive", "Online", "Offline"); 可能会更好,因为您不想删除table-row-cell 等默认样式类。
  • 这也可以,谢谢
  • this also works 相当颠倒:由于@James_D 指出的原因,您的解决方案是错误的(即使它似乎按预期工作) - 请更改为 James 的建议不要混淆未来的读者:)
  • 这么好?我用评论改变了它
猜你喜欢
  • 2018-02-17
  • 1970-01-01
  • 2013-02-18
  • 2020-01-02
  • 2014-02-07
  • 2011-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多