在 TableView 内滚动和单击时,我注意到一个奇怪的行为。
基本上每个带有“Missing”文本的单元格都应该有红色字体颜色。启动应用程序时,一切正常并且正常工作: Base Case
双击复选框并向上滚动(现在第一行位于 View 中)后,第一行的字体为红色: 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.getAddress != null) {
setText(item);
setStyle(null);
} else {
setText("Missing");
setStyle("-fx-text-fill: red");
}
关于java - 在 JavaFX 中更新 TableCell 时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62373846/