加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – :选择的伪类样式不适用于单元格

发布时间:2020-12-15 04:33:46 所属栏目:Java 来源:网络整理
导读:我在场景中有一些TableView,我想要突出显示所选的单元格.根据 JavaFX CSS reference,有一个伪类:在Cells上选择,所以我尝试了以下css: .cell:selected { -fx-effect: dropshadow(gaussian,10,.2,4,4);} 但风格不适用于细胞.当我使用.cell时:悬停它按预期工
我在场景中有一些TableView,我想要突出显示所选的单元格.根据 JavaFX CSS reference,有一个伪类:在Cells上选择,所以我尝试了以下css:

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

但风格不适用于细胞.当我使用.cell时:悬停它按预期工作.

以下是简化的FXML:

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

我正在使用它作为控制器:

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);
    }
}

为什么CSS不能应用于选定的单元格?

解决方法

这里的问题是JavaFX处理单元格和行的选择的方式.

让我们暂时参考TableViewSelectionModel的javadoc,特别是cellSelectionEnabled属性:

A boolean property used to represent whether the table is in row or cell selection modes. By default a table is in row selection mode which means that individual cells can not be selected. Setting cellSelectionEnabled to be true results in cells being able to be selected (but not rows).

我们可以得出结论,您的单元格未标记为已选中,因为您处于行选择模式.
你可以通过调整你的css选择器来依赖行(这样的事情)来解决这个问题:

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

您可以结合以下内容使其更有用:CellView选择和:TableView上的行选择:

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

无论TableViewSelectionModel的操作方式如何,都将应用于选定的单元格

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读