Java –: the selected pseudo class style does not apply to cells

I have some tableviews in the scene. I want to highlight the selected cells According to JavaFX CSS reference, there is a pseudo class: select on cells, so I tried the following CSS:

.cell:selected {
    -fx-effect: dropshadow(gaussian,10,.2,4,4);
}

But style does not apply to cells When I use When hovering over a cell: it works as expected

The following is a simplified fxml:

<Pane fx:controller="Controller">
   <children>
        <TableView fx:id="table" />
   </children>
</Pane>

I'm using it as a controller:

public class Controller implements Initializable{
    @FXML
    private TableView<SomeClass> table;
    // some other things

    @Override
    public void initialize(URL url,ResourceBundle bundle) {
        Objects.requireNonNull(table,"Table was not injected");
        // create columns,initialize other stuff
        table.getColumns().clear();
        table.getColumns().addAll(/*some columns */);
        table.setEditable(false);
        table.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
        table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
    }
}

Why can't CSS be applied to the selected cells?

Solution

The problem here is how JavaFX handles the selection of cells and rows

Let's temporarily refer to the Javadoc of tableviewselectionmodel, especially the cellselectionenabled property:

We can conclude that your cell is not marked as selected because you are in row selection mode You can solve this problem by adjusting your CSS selector to rely on rows (such things):

.table-row-cell:selected .cell {
    -fx-effect: ...;
}

You can combine the following to make it more useful: cellview selection and: row selection on tableview:

.table-view:row-selection .table-row-cell:selected .cell,.table-view:cell-selection .cell:selected {
    -fx-effect: ...;
}

The tableviewselectionmodel is applied to the selected cells regardless of how it operates

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>