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

时间:2018-12-09 01:13:37

标签: java javafx kotlin tornadofx

我在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 :(得分:1)

我不是专家。我不知道是否有一种方法可以使用您的确切方法(使用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)
            }
        }
    }
}

现在,您使用规则“通过”和“失败”。代替:

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)     
}
相关问题