我需要你的帮助!
我有一张有行的表格(名字等)现在,当放置在该行上的对象具有特定值时,我希望为特定的tableCells背景着色。但我只知道这个细胞的价值。但是我需要读取对象(在我的代码中称为TableListObject),才能知道我需要对单元格进行颜色化。但该“颜色值”在该行中不可见(没有列)。
这是我的代码:
for(TableColumn tc:tView.getColumns()) {
if(tc.getId().equals("text")) {
tc.setCellValueFactory(newPropertyValueFactory<TableListObject,String>("text"));
// here i need to check the Objects value and coloring that cell
}
}这里有一个HTML来可视化我的问题:https://jsfiddle.net/02ho4p6e/
发布于 2016-01-28 09:42:08
为所需的列调用单元格工厂,并重写updateItem方法。您需要检查它是否为空,如果不是,则可以进行对象检查,然后可以设置单元格背景的颜色或任何其他样式。希望这能有所帮助。
tc.setCellFactory(column -> {
return new TableCell<TableListObject, String>() {
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
setStyle("");
} else {
if (item.equals("Something")) {
setStyle("-fx-background-color: blue");
} else {
setStyle("");
}
}
}
};
});编辑1:
如果要使用同一行中另一个单元格的值,请执行以下操作。您必须使用行的索引,并获得检查所需的项。
tc.setCellFactory(column - > {
return new TableCell < TableListObject, String > () {
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
setStyle("");
} else {
int rowIndex = getTableRow().getIndex();
String valueInSecondaryCell = getTableView().getItems().get(rowIndex).getMethod();
if (valueInSecondaryCell.equals("Something Else")) {
setStyle("-fx-background-color: yellow"); //Set the style in the first cell based on the value of the second cell
} else {
setStyle("");
}
}
}
};
});编辑2:
根据建议改进答案。这使用引用的对象。
else {
TableListObject listObject = (TableListObject) getTableRow().getItem();
if (listObject.getMethod().equals("Something Else")) {
setStyle("-fx-background-color: yellow"); //Set the style in the first cell based on the value of the second cell
} else {
setStyle("");
}
}https://stackoverflow.com/questions/35056108
复制相似问题