首页 文章

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

提问于
浏览
0

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

如何更改此格式以突出显示所选行并为行着色以反映该测试是通过还是失败?

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

1 回答

  • 0

    我不是专家 . 我不知道是否有办法使用您的确切方法回答您的问题(使用inlinecss并设置backgroundColor而不影响选定的行backgroundColor) . 我的解决方案使用StyleSheet并为行的选定状态设置独立的backgroundColor .

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

    现在您使用规则“pass”和“fail” . 代替:

    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添加到应用程序构造函数中 .

    Edit:

    使用toggleClass作为Edvin Syse建议 . 代替:

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

相关问题