我注意到在我的TableView中滚动和点击时有一个奇怪的行为。
基本上每个“缺少”文本的单元格都应该有红色字体颜色。 当启动应用程序时,一切都很好并且正常工作:基本情况
双击复选框并向上滚动(现在第一行在视图中)后,第一行的字体为红色:第一行红色
向下滚动后,最后一行的字体为红色:最后一行红色
这就是我使用的代码:
tcAddressAlias.setCellFactory(tc -> new TableCell<Account, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
Account a = getTableView().getItems().get(getIndex());
if(a.getAddress() != null) {
setText(item);
} else {
setText("Missing");
setStyle("-fx-text-fill: red");
}
}
}
});
tcAddressAlias.setCellValueFactory(tc ->
Optional.ofNullable(tc.getValue().getAddress())
.map(add -> new SimpleStringProperty(add.getAlias()))
.orElseGet(() -> new SimpleStringProperty(""))
);
我已经试过调试了,但一切似乎都很好。。。
谢谢@迈克尔逊! 我补充道
setStyle(null);
示例:
if(a.getCreditCard() != null) {
setText(item);
setStyle(null);
} else {
setText("Missing");
setStyle("-fx-text-fill: red");
}