JavaFX仅在以下情况下将css应用于TableRows:启用

GOX*_*LUS 6 css java javafx

假设JavaFX CSS不支持:enabled基于JavaFX 8的Oracle CSS参考我如何能够做到以下几点?

仅在未禁用时悬停和激活


一个TableView存在在那里我申请某种形式的CSS的:

.table-row-cell:hover {
    -fx-background-color:orange;
}

.table-row-cell:focused {
    -fx-background-color:purple;
}
Run Code Online (Sandbox Code Playgroud)

我希望在TableRow启用时(仅)完成上述操作.

所以我修改了包含:enabled伪元素的代码,但现在没有任何作用:

.table-row-cell:hover:enabled {
    -fx-background-color:orange;
}

.table-row-cell:focused:enabled {
    -fx-background-color:purple;
}
Run Code Online (Sandbox Code Playgroud)

最后一些小问题:

1)如何我可以结合:悬停:禁用:启用

->[ apply **:hover** only if the `Table-Row|Cell` is enabled. ]
Run Code Online (Sandbox Code Playgroud)

2)JavaFX css是否支持:启用


最后但并非最不重要:

在对上面的代码进行了几次测试后,我得到了youtu.be/l7Pbz2l2wjE?t=138这个结果.

GOX*_*LUS 1

我最终使用的解决方案:disabled如下:

正如您可以清楚地看到的那样,该行可以是focusedbutno hovered甚至即使selected:disabled


解决方案 1 :disabled(更快):

 /* .table-row-cell */

.table-row-cell:disabled{
    -fx-opacity:0.5;
}

.table-row-cell .text{
    -fx-font-weight:bold;
    -fx-fill: black ;
} 

.table-row-cell:focused .text {
     -fx-fill: white ;
}

.table-row-cell:focused{
    -fx-background-color:purple;
}

.table-row-cell:focused:disabled{ /* focused+disabled */
    -fx-background-color:blue;
}

.table-row-cell:hover{
    -fx-background-color:magenta;
}
Run Code Online (Sandbox Code Playgroud)

解决方案 2 基于Harshita Sethi的答案,使用pseudoclass

PseudoClass enableRowClass = PseudoClass.getPseudoClass("enabled-row");

setRowFactory(tv -> {
                TableRow<Media> row = new TableRow<>();

                // use EasyBind to access the valueProperty of the itemProperty
                // of the cell:
                row.disableProperty().bind(
                        // start at itemProperty of row
                        EasyBind.select(row.itemProperty())
                                // map to fileExistsProperty[a boolean] of item, if item
                                // non-null
                                .selectObject(Media::fileExistsProperty)
                                // map to BooleanBinding checking if false
                                .map(x -> x.booleanValue() == false)
                                // value to use if item was null
                                .orElse(false));

  //This line of code is the idea of the `Harshita Sethi` modified a little bit to not use a changelistener
  row.pseudoClassStateChanged(enableRowClass, !row.disabledProperty().get());

    return row;
 });
Run Code Online (Sandbox Code Playgroud)
  • 当然还有.css(请注意,它不会产生与解决方案 1 完全相同的结果,因为它缺少一些行;):

    .table-row-cell:enabled-row:hover .table-cell {
        -fx-background-color: purple;
    
    }
    
    .table-row-cell:enabled-row:focused .table-cell {
        -fx-background-color: orange;
    
    }
    
    Run Code Online (Sandbox Code Playgroud)