I have a label in a view that is controlled by a integer property, when the value is negative it displays with a minus sign, when the value is positive it does not. However, I would like the Label to display "+5", "-3" ...
take the following code as an example
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
}
}
Is there a way I can format the text once the property changes ? Thank you.
You could create a stringbinding which holds the value you want to display in the label and then bind the label's value property to that:
val prop = SimpleIntegerProperty(1)
val propDesc = prop.stringBinding { "%+d".format(it) }
Now you can do:
label(propDesc)
The label will update whenever the property changes value.
You can of course also inline it:
label(prop.stringBinding { "%+d".format(it) })