我在 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/