java - 更改 TornadoFX TableView 行背景颜色,同时仍突出显示所选行

标签 java javafx kotlin tornadofx

我在 TornadoFX 应用程序中有一个 TableView。此 TableView 显示测试列表及其状态(未开始、已开始、通过、失败)。我希望通过的测试的行为绿色,失败的测试的行为红色。我已经使行的颜色正确,但是当我在表中选择一行时,它不再突出显示。

如何更改此格式以突出显示所选行并为行着色以反射(reflect)测试是否通过或失败?

tableview = tableview(tests) {
    readonlyColumn("Test Name", Test::fileName)
    column("Test Execution Status", Test::statusProperty).cellFormat {
        text = it.toString()
        if (it == TestStatus.PASS)
            this.tableRow.style(append = true) { backgroundColor += c("#4CAF50", .5) }
        else if (it == TestStatus.FAIL)
            this.tableRow.style(append = true) { backgroundColor += c("#FF5722", .5) }
    }

    columnResizePolicy = SmartResize.POLICY
    vgrow = Priority.ALWAYS
    selectionModel.selectionMode = SelectionMode.MULTIPLE
    bindSelected(lastSelectedTestInTable)
}

最佳答案

我不是专家。我不知道是否有办法使用您的确切方法(使用 inlinecss 并设置背景颜色而不影响所选行背景颜色)来回答您的问题。我的解决方案使用样式表并为行的选定状态设置独立的背景颜色。

class Style : Stylesheet() {
    companion object {
        val pass by cssclass()
        val fail by cssclass()
    }
    init {
        pass{
            backgroundColor += c("#4CAF50", .5)
            and(selected){
                backgroundColor += c("#0096C9", .5)
            }
        }
        fail{
            backgroundColor += c("#FF5722", .5)
            and(selected){
                backgroundColor += c("#0096C9", .5)
            }
        }
    }
}

现在您使用规则“通过”和“失败”。而不是:

this.tableRow.style(append = true) { backgroundColor += c("#4CAF50", .5) }

您使用:

this.tableRow.addClass(Style.pass)

而不是:

this.tableRow.style(append = true) { backgroundColor += c("#FF5722", .5) }

您使用:

this.tableRow.addClass(Style.fail)

请记住,您需要将 Style::class 添加到应用程序构造函数中。

编辑:

按照Edvin Syse的建议使用toggleClass。而不是:

column("Test Execution Status", Test::statusProperty).cellFormat {
    text = it.toString()
    if (it == TestStatus.PASS)
        this.tableRow.addClass(Style.pass)
    else if (it == TestStatus.FAIL)
        this.tableRow.addClass(Style.fail)
}

您使用:

column("Test Execution Status", Test::statusProperty).cellFormat {
    text = it.toString()
    this.tableRow.toggleClass(Style.fail,it == TestStatus.FAIL)
    this.tableRow.toggleClass(Style.pass,it == TestStatus.PASS)     
}

关于java - 更改 TornadoFX TableView 行背景颜色,同时仍突出显示所选行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53688552/

相关文章:

java - 在亚马逊云搜索上上传文件?

java - 一遍又一遍地使用相同的 Hibernate Session 是一种好习惯吗

generics - 我们可以在 Kotlin 中使用中缀泛型方法吗?

java - 在服务器硬件上播放音频

java - 使用 Maven 生成 JavaFX 项目

spring - Spring 代理类和 Kotlin 中的空指针异常

kotlin - takeWhile 包含与谓词匹配的实际值 (takeWhileInclusive)

java - Teradata JDBC 准备好的语句错误

java - 使用正则表达式从字符串中提取键值对

java - 如何存储要在多个参与者之间共享的数据?