我有一个填充有 Samples 对象的表格视图。如果 Samples 对象的属性 = true,我想填充 TableRow。我使用了基于jewelsea这个答案的方法:Disable TableRow based on data
int i = 0;
if (tblSamples.getItems().size() > 0) {
for (Node n : tblSamples.lookupAll("TableRow")) {
if (n instanceof TableRow) {
TableRow row = (TableRow) n;
row.getStyleClass().removeAll("hasMastitisRow");
if (tblSamples.getItems().get(i).getClinicalMastitis()) {
row.getStyleClass().add("hasMastitisRow");
}
i++;
if (i == tblSamples.getItems().size()) {
break;
}
}
}
}
CSS:
.hasMastitisRow {
-fx-control-inner-background: linear-gradient(#FFFFFF 85%, red 100%);
}
但是,它只适用于奇数行,偶数行似乎不起作用?
编辑:当CSS应用于偶数行时,我似乎也遇到了以下错误(在奇数行上我没有错误):
WARNING: com.sun.javafx.css.StyleHelper calculateValue Could not resolve '-fx-table-cell-border-color' while resolving lookups for '-fx-background-color' from rule '*.table-row-cell:odd' in stylesheet jar:file:/C:/Program%20Files/Java/jdk1.7.0_15/jre/lib/jfxrt.jar!/com/sun/javafx/scene/control/skin/caspian/caspian.bss
WARNING: com.sun.javafx.css.StyleHelper calculateValue Could not resolve '-fx-control-inner-background' while resolving lookups for '-fx-text-fill' from rule '*.table-cell' in stylesheet jar:file:/C:/Program%20Files/Java/jdk1.7.0_15/jre/lib/jfxrt.jar!/com/sun/javafx/scene/control/skin/caspian/caspian.bss
WARNING: com.sun.javafx.css.StyleHelper calculateValue Could not resolve '-fx-control-inner-background' while resolving lookups for '-fx-text-fill' from rule '*.table-cell' in stylesheet jar:file:/C:/Program%20Files/Java/jdk1.7.0_15/jre/lib/jfxrt.jar!/com/sun/javafx/scene/control/skin/caspian/caspian.bss
WARNING: com.sun.javafx.css.StyleHelper calculateValue Could not resolve '-fx-control-inner-background' while resolving lookups for '-fx-text-fill' from rule '*.table-cell' in stylesheet jar:file:/C:/Program%20Files/Java/jdk1.7.0_15/jre/lib/jfxrt.jar!/com/sun/javafx/scene/control/skin/caspian/caspian.bss
WARNING: com.sun.javafx.css.StyleHelper calculateValue Could not resolve '-fx-control-inner-background' while resolving lookups for '-fx-text-fill' from rule '*.table-row-cell' in stylesheet jar:file:/C:/Program%20Files/Java/jdk1.7.0_15/jre/lib/jfxrt.jar!/com/sun/javafx/scene/control/skin/caspian/caspian.bss
WARNING: com.sun.javafx.css.StyleHelper calculateValue Could not resolve '-fx-table-cell-border-color' while resolving lookups for '-fx-background-color' from rule '*.table-row-cell:odd' in stylesheet jar:file:/C:/Program%20Files/Java/jdk1.7.0_15/jre/lib/jfxrt.jar!/com/sun/javafx/scene/control/skin/caspian/caspian.bss
WARNING: com.sun.javafx.css.StyleHelper calculateValue Could not resolve '-fx-control-inner-background' while resolving lookups for '-fx-text-fill' from rule '*.table-cell' in stylesheet jar:file:/C:/Program%20Files/Java/jdk1.7.0_15/jre/lib/jfxrt.jar!/com/sun/javafx/scene/control/skin/caspian/caspian.bss
WARNING: com.sun.javafx.css.StyleHelper calculateValue Could not resolve '-fx-control-inner-background' while resolving lookups for '-fx-text-fill' from rule '*.table-cell' in stylesheet jar:file:/C:/Program%20Files/Java/jdk1.7.0_15/jre/lib/jfxrt.jar!/com/sun/javafx/scene/control/skin/caspian/caspian.bss
WARNING: com.sun.javafx.css.StyleHelper calculateValue Could not resolve '-fx-control-inner-background' while resolving lookups for '-fx-text-fill' from rule '*.table-cell' in stylesheet jar:file:/C:/Program%20Files/Java/jdk1.7.0_15/jre/lib/jfxrt.jar!/com/sun/javafx/scene/control/skin/caspian/caspian.bss
编辑: 新方法但结果仍然相同(应用于奇数行,而不是偶数行):
tc_Samples_Quarter.setCellFactory(new Callback<TableColumn<Samples, String>, TableCell<Samples, String>>() {
@Override
public TableCell<Samples, String> call(TableColumn<Samples, String> soCalledSampleRowStringTableColumn) {
return new TableCell<Samples, String>() {
@Override
public void updateItem(final String item, final boolean empty) {
super.updateItem(item, empty);
this.getTableRow().getStyleClass().remove("hasMastitisRow");
if (item != null) {
setText(item);
this.getTableRow().getStyleClass().add("hasMastitisRow");
} else {
setText(null);
}
}
};
}
});