如何在 TableView TableCell 中对每个字符进行不同的着色
How to color each character differently in a TableView TableCell
我有一个表格视图,在一列中我希望能够单独为字符串的每个字符着色。显然,textFill 仅适用于整个单元格,因为我确实尝试先按字符拆分字符串。我在下面包含了代码,并不完全如我所愿,但它是指示性的(它仅根据第一个字符更改单元格文本颜色)。我如何调整才能在该单元格中获得彩色输出? [注:unicode字符为上下箭头]
val colTick = new TableColumn[Quote, String] {
editable = false
text = "Tick"
prefWidth = 90
alignmentInParent = scalafx.geometry.Pos.Center
cellFactory = { _ =>
new TableCell[Quote, String] {
item.onChange { (_, _oldTick, newTick) =>
if (newTick == null) {
text = null
graphic = null
} else {
if (newTick(0) == '\u2B06') {
textFill = Color.Green
} else {
if (newTick(0) == '\u2B07') textFill = Color.Red else textFill = Color.Black
}
text = newTick
}
}
}
}
cellValueFactory = {
_.value.tick
}
}
一个选项是为每个字符使用单独的 Text
,这样您就可以单独设置其颜色。然后将 Text
组合到 HBox
、TextFlow
或任何合适的地方。这是一个示例,其中字符 r
、g
和 b
为彩色,其他字符为黑色:
cellFactory = { _ =>
new javafx.scene.control.TableCell[Quote, String] {
override def updateItem(item: String, empty: Boolean): Unit = {
super.updateItem(item, empty)
setText(null)
if (item == null || empty) {
setGraphic(null)
} else {
val texts = item.map { c =>
val color: Color = c.toLower match {
case 'r' => Color.Red
case 'g' => Color.Green
case 'b' => Color.Blue
case _ => Color.Black
}
new Text {
text = c.toString
fill = color
}
}
setGraphic(new HBox(texts: _*))
}
}
}
}
从@Jarek 给出的评论开始,这段代码现在似乎可以工作了:
cellFactory = { _ =>
new TableCell[Quote, String] {
item.onChange { (_, _oldTick, newTick) =>
if (newTick == null) {
text = null
graphic = null
} else {
val texts = newTick.map { c =>
val color: Color = c match {
case '⬇' => Color.Red
case '⬆' => Color.Green
case '_' => Color.Crimson
case '○' => Color.DarkBlue
case _ =>Color.Black
}
new Text {
setText(c.toString)
setFill(color)
}
}
graphic = new javafx.scene.layout.HBox(texts: _*)
}
}
}
}
我有一个表格视图,在一列中我希望能够单独为字符串的每个字符着色。显然,textFill 仅适用于整个单元格,因为我确实尝试先按字符拆分字符串。我在下面包含了代码,并不完全如我所愿,但它是指示性的(它仅根据第一个字符更改单元格文本颜色)。我如何调整才能在该单元格中获得彩色输出? [注:unicode字符为上下箭头]
val colTick = new TableColumn[Quote, String] {
editable = false
text = "Tick"
prefWidth = 90
alignmentInParent = scalafx.geometry.Pos.Center
cellFactory = { _ =>
new TableCell[Quote, String] {
item.onChange { (_, _oldTick, newTick) =>
if (newTick == null) {
text = null
graphic = null
} else {
if (newTick(0) == '\u2B06') {
textFill = Color.Green
} else {
if (newTick(0) == '\u2B07') textFill = Color.Red else textFill = Color.Black
}
text = newTick
}
}
}
}
cellValueFactory = {
_.value.tick
}
}
一个选项是为每个字符使用单独的 Text
,这样您就可以单独设置其颜色。然后将 Text
组合到 HBox
、TextFlow
或任何合适的地方。这是一个示例,其中字符 r
、g
和 b
为彩色,其他字符为黑色:
cellFactory = { _ =>
new javafx.scene.control.TableCell[Quote, String] {
override def updateItem(item: String, empty: Boolean): Unit = {
super.updateItem(item, empty)
setText(null)
if (item == null || empty) {
setGraphic(null)
} else {
val texts = item.map { c =>
val color: Color = c.toLower match {
case 'r' => Color.Red
case 'g' => Color.Green
case 'b' => Color.Blue
case _ => Color.Black
}
new Text {
text = c.toString
fill = color
}
}
setGraphic(new HBox(texts: _*))
}
}
}
}
从@Jarek 给出的评论开始,这段代码现在似乎可以工作了:
cellFactory = { _ =>
new TableCell[Quote, String] {
item.onChange { (_, _oldTick, newTick) =>
if (newTick == null) {
text = null
graphic = null
} else {
val texts = newTick.map { c =>
val color: Color = c match {
case '⬇' => Color.Red
case '⬆' => Color.Green
case '_' => Color.Crimson
case '○' => Color.DarkBlue
case _ =>Color.Black
}
new Text {
setText(c.toString)
setFill(color)
}
}
graphic = new javafx.scene.layout.HBox(texts: _*)
}
}
}
}