【问题标题】:Change TornadoFX TableView row background color while still highlighting selected rows更改 TornadoFX TableView 行背景颜色,同时仍突出显示选定行
【发布时间】:2019-05-10 08:13:43
【问题描述】:

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

【问题讨论】:

    标签: java javafx kotlin tornadofx


    【解决方案1】:

    我不是专家。我不知道是否有办法使用您的确切方法来回答您的问题(使用 inlinecss 并设置背景颜色而不影响选定的行背景颜色)。我的解决方案使用 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)
                }
            }
        }
    }
    

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

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

    【讨论】:

    • 非常好@gredow - 这是要走的路。你可以使用 toggleClass 让它变得更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    相关资源
    最近更新 更多