CSS仅适用于奇数行

时间:2013-03-03 11:04:25

标签: java css javafx-2

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

2 个答案:

答案 0 :(得分:0)

对您的部分代码感到好奇。首先,您将获取节点,如:

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

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

if (n instanceof TableRow) {

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

答案 1 :(得分:0)

使用setCellFactory解决方案(参见上面问题中的其他方法)并将CSS调整为:

.hasMastitisRow {  
    -fx-background-color: linear-gradient(#FFFFFF 85%, red 100%);
}