标签TornadoFX的格式文本

时间:2019-03-13 12:07:34

标签: javafx kotlin tornadofx

我在一个由整数属性控制的视图中有一个标签,当该值为负数时,它将显示一个负号,而当该值为正数时,它将没有负号。但是,我希望标签显示“ +5”,“-3” ...

以以下代码为例

import javafx.beans.property.SimpleIntegerProperty
import tornadofx.*

class MyView : View() {

   val negProp = SimpleIntegerProperty(-3) // this prop is in a ItemViewModel
   val posProp = SimpleIntegerProperty(+4) // this prop is in a ItemViewModel

    override val root = hbox {
        label(negProp)      // shows - 3
        label(posProp)      // shows 4
    }
}

一旦属性更改,是否可以格式化文本? 谢谢。

1 个答案:

答案 0 :(得分:3)

您可以创建一个字符串绑定,其中包含要在标签中显示的值,然后将标签的value属性绑定到该字符串:

val prop = SimpleIntegerProperty(1)
val propDesc = prop.stringBinding { "%+d".format(it) }

现在您可以这样做:

label(propDesc)

只要属性更改值,标签就会更新。

您当然也可以内联它:

label(prop.stringBinding { "%+d".format(it) })