1

我有一个填充有 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);
                    }
                }
            };
        }
    });
4

2 回答 2

0

对你的一些代码感到好奇。首先,您正在获取您的节点,例如:

for (Node n : tblSamples.lookupAll("TableRow")) {

然后你做一个检查实例,看看它是否是一个 TableRow

if (n instanceof TableRow) {

有必要吗?lookupAll是否有可能返回不是 TableRow 的节点?如果是这样,那么一种可能性可能是i索引的递增仅发生在if (n instanceof TableRow)内,因此i仅在它是 TableRow 时才递增。会不会是i不匹配索引中的项目,因为它跟不上循环?

于 2013-03-03T14:12:56.470 回答
0

这已通过使用 setCellFactory 解决方案(请参阅上述问题中的其他方法)并将 CSS 调整为:

.hasMastitisRow {  
    -fx-background-color: linear-gradient(#FFFFFF 85%, red 100%);
}
于 2013-04-10T17:40:37.957 回答