java - TableView Cell 跳过颜色
问题描述
各个单元格的颜色不是我指定的颜色。
带有“在线”的每一行应该是白色的,带有“离线”的每一行应该是略带绿色的。这一切都有效,但是当我向下和向上滚动时,“在线”上的那些将绿色作为背景色......
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”没有落后。
解决方案
好的,我自己解决了这个问题。我只是简单地添加了 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");
}
}
});
推荐阅读
- python - 如何检查一个变量的两个条件?
- fabricjs - fabricjs 剪辑路径不按尺寸工作
- vb.net - Tapi 与团队/其他 cisco 系统一起使用
- javascript - ESLint:让它理解///
VS Code 中有什么方法可以让 ESLint 理解
/// <reference path
指令,这样它就不会no-undef
为引用的 JS 文件中定义的变量/函数抛出错误?它似乎以某种方式工作,但不完全:VS Code function descri
- php - 获取当前路由规则
- bash - 遍历给定文件夹的文件 - Bash
- jquery - 按文本而不是它的值对数据表列进行排序
- grpc - 这对 proto 文件来说是一个糟糕的结构吗
- c# - 父实体上的软删除,但子/关系 EF Core 上的硬删除
- python - 无法同时运行烧瓶和 scapy