我有以下代码来更新列单元格及其相应行的颜色:
calltypel.setCellFactory(column -> {
return new TableCell<CallLogs, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? "" : getItem().toString());
setGraphic(null);
TableRow currentRow = getTableRow();
//This doesn't work
if(item.equals("a")){
item.setTextFill(Color.RED);
currentRow.setTextFill(Color.PINK);
}
else{
item.setTextFill(Color.GREEN);
currentRow.setTextFill(Color.BLUE);
}
}
};
});
“if”条件的代码段不起作用。我无法识别对对象的正确引用以及执行此操作的最佳方法是什么。
谢谢!
最佳答案
private void customiseFactory(TableColumn<CallLogs, String> calltypel) {
calltypel.setCellFactory(column -> {
return new TableCell<CallLogs, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? "" : getItem().toString());
setGraphic(null);
TableRow<CallLogs> currentRow = getTableRow();
if (!isEmpty()) {
if(item.equals("a"))
currentRow.setStyle("-fx-background-color:lightcoral");
else
currentRow.setStyle("-fx-background-color:lightgreen");
}
}
};
});
}
这有效!
关于Javafx:TableView根据列值更改行颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30889732/