Java 更改FX TableView行背景色,同时仍高亮显示选定行
我在TornadoFX应用程序中有一个TableView。此表视图显示测试及其状态(未启动、已启动、通过、失败)的列表。我希望通过测试的行为绿色,失败测试的行为红色。我已将行设置为正确的颜色,但当我在表中选择一行时,它不再高亮显示 如何更改此格式以突出显示所选行并对行着色以反映测试是否通过Java 更改FX TableView行背景色,同时仍高亮显示选定行,java,javafx,kotlin,tornadofx,Java,Javafx,Kotlin,Tornadofx,我在TornadoFX应用程序中有一个TableView。此表视图显示测试及其状态(未启动、已启动、通过、失败)的列表。我希望通过测试的行为绿色,失败测试的行为红色。我已将行设置为正确的颜色,但当我在表中选择一行时,它不再高亮显示 如何更改此格式以突出显示所选行并对行着色以反映测试是否通过 tableview = tableview(tests) { readonlyColumn("Test Name", Test::fileName) column("Test Executio
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.style(append = true) { backgroundColor += c("#FF5722", .5) }
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)
}
您使用:
this.tableRow.addClass(Style.pass)
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)
}
而不是:
this.tableRow.style(append = true) { backgroundColor += c("#4CAF50", .5) }
this.tableRow.style(append = true) { backgroundColor += c("#FF5722", .5) }
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)
}
您使用:
this.tableRow.addClass(Style.pass)
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)
}
请记住,您需要将Style::class添加到应用程序构造函数中
编辑:
使用Edvin Syse建议的toggleClass。而不是:
this.tableRow.style(append = true) { backgroundColor += c("#4CAF50", .5) }
this.tableRow.style(append = true) { backgroundColor += c("#FF5722", .5) }
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)
}
您使用:
this.tableRow.addClass(Style.pass)
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使它更漂亮。