JavaFX TableView更改选定的单元格颜色 [英] JavaFX TableView change selected cell colour

查看:3287
本文介绍了JavaFX TableView更改选定的单元格颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaFX TableView和一个ListView,它们都使用自定义单元工厂。特别是我已经重写了updateItem方法,以便根据单元格值绑定特定的CSS类。

I have a JavaFX TableView and a ListView and they both use custom cell factories. In particular I have overridden updateItem method in order to bind a particular CSS class based on cell value.

这是我的CSS文件的一部分:

This is part of my CSS file:

.tissueCell {
    -fx-text-fill: #F5AD11;
}

.tissueCell:selected {
    -fx-background-color: #F5AD11;
    -fx-text-fill: white;
}

.significantDataCell {
    -fx-background-color: yellow;
    -fx-text-fill: black;
}

.significantDataCell:selected {
    -fx-background-color: white;
    -fx-text-fill: black;
}

对于ListView,一切都完美无缺:文本以正确的颜色显示选择单元格后,文本变为白色,背景填充正确的颜色。

For the ListView everything work flawlessly: text is displayed with the proper colour and when the cell is selected the text becomes white and the background is filled with proper colour.

我遇到了TableView的问题。未选中时,单元格中的文本将以所选颜色显示,但是当选择单元格时,背景将使用默认的JavaFX颜色填充选定的表格单元格背景,文本颜色仍为#F5AD11(它不会变为白色)。

I am experiencing problems with the TableView instead. When unselected the text in the cell is displayed with the chosen colour, but when the cell is selected the background is filled with default JavaFX colour for selected table cells background and the text colour remains #F5AD11 (it does not become white).

使用.significantDataCell类的TableCells也是如此。单元格正确显示黄色背景和黑色文本,但选中时没有任何变化,这次不会出现背景事件。

The same happens with TableCells that use .significantDataCell class. Cells are displayed properly with yellow background and black text, but when selected nothing changes, not event the background this time.

有什么想法吗?我做了很多研究,但找不到任何可行的解决方案。

Any ideas? I did a lot of research but couldn't find any working solution.

推荐答案

默认情况下, TableView s不允许选择单个单元格,但允许选择行。因此,选择器 .table-cell:selected 永远不会匹配默认选择模式中的任何单元格。在这种情况下,您需要

By default, TableViews do not allow selection of individual cells, but allow selection of rows. Thus the selector .table-cell:selected never matches any cell in the default selection mode. In this case, you would need

.table-row-cell:selected .table-cell {
    /* style definitions */
}

或在您的方案中

.table-row-cell:selected .tissue-cell {
    -fx-background-color: #F5AD11;
    -fx-text-fill: white;
}

等。

如果允许表格使用单元格选择,则通过调用

If you allow the table to use cell selection, by calling

myTableView.setCellSelectionEnabled(true);

然后在单击鼠标(等)时选择单个单元格,因此原始CSS将起作用。

then individual cells become selected on mouse click (etc), and so your original CSS will work.

这篇关于JavaFX TableView更改选定的单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆