[在滚动查看并单击TableView时,我发现了奇怪的行为。
基本上每个带有“ Missing”文本的单元格都应使用红色字体。启动应用程序时,一切正常,并且可以正常工作:Base Case
双击复选框并向上滚动(现在第一行在视图中)后,第一行的字体为红色:First line red
向下滚动后,最后一行的字体为红色:Last line red
这是我正在使用的代码:
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(""))
);
我已经尝试过调试,但是一切似乎都很好...
感谢@michelson!我加了
setStyle(null);
示例:
if(a.getCreditCard() != null) {
setText(item);
setStyle(null);
} else {
setText("Missing");
setStyle("-fx-text-fill: red");
}