【发布时间】: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 样式类可以添加多次,因此它们倾向于如果没有完全清洁会堆积)
-
我该如何清理这个?问题是当我向下滚动表格时,实际的白色字段是绿色的。