JavaFX着色表细胞
我需要你的帮助 我有一个表,表中有行(名称等) 现在,我想给一个特定的tableCells背景上色,当这个行上的对象有一个特定的值时。但我只能读取这个单元格的值。但是我需要读取对象(在我的代码中称为JavaFX着色表细胞,java,javafx,background-color,tablecell,Java,Javafx,Background Color,Tablecell,我需要你的帮助 我有一个表,表中有行(名称等) 现在,我想给一个特定的tableCells背景上色,当这个行上的对象有一个特定的值时。但我只能读取这个单元格的值。但是我需要读取对象(在我的代码中称为TableListObject)才能知道我需要用哪种颜色给单元格上色。但该“颜色值”在该行中不可见(没有列) 这是我的密码: for(TableColumn tc:tView.getColumns()) { if(tc.getId().equals("text")) { tc.
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
}
}
(TableColumn tc:tView.getColumns())的{
if(tc.getId().equals(“text”)){
tc.setCellValueFactory(newPropertyValueFactory(“文本”);
//在这里,我需要检查对象的值,并为该单元格着色
}
}
下面是一个HTML提琴,用于可视化我的问题:
调用所需列的单元格工厂,并重写
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("");
}
}
}
};
});
非常感谢。我已经有了,但问题是:我需要检查的值不在“item”字符串中。因为“item”字符串只是当前单元格的值。但是我需要检查的值在RowObject中(且不在任何单元格中),是否要为整行着色?不,仅为该行的“文本”单元格。如果项目不是字符串,则可以将其更改为任何类型。如日期等。当然,但问题是,我需要检查的值在该行中,但不在我要着色的单元格中。相关,但略有不同:
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("");
}
}
}
};
});
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("");
}
}