Douglas K. N.
Douglas K. N.

Reputation: 43

How do I bind a custom property to a textfield bidirectionally?

I have a complex object that I want to display in a textfield. This is working fine with a stringBinding. But I don't know how to make it two-way so that the textfield is editable.

package com.example.demo.view

import javafx.beans.property.SimpleObjectProperty
import javafx.beans.property.SimpleStringProperty
import tornadofx.*

class MainView : View("Hello TornadoFX") {
    val complexThing: Int = 1
    val complexProperty = SimpleObjectProperty<Int>(complexThing)

    val complexString = complexProperty.stringBinding { complexProperty.toString() }

    val plainString = "asdf"
    val plainProperty = SimpleStringProperty(plainString)

    override val root = vbox {
        textfield(complexString)
        label(plainProperty)
        textfield(plainProperty)
    }
}

When I run this, the plainString is editable and I see the label change because the edits are going back into the property.

How can I write a custom handler or what class do I need to use to make the stringBinding be read and write? I looked through a lot of the Property and binding documentation but did not see anything obvious.

Upvotes: 0

Views: 541

Answers (2)

Steph
Steph

Reputation: 841

Ta-Da

class Point(val x: Int, val y: Int) //You can put properties in constructor

class PointConverter: StringConverter<Point?>() {
    override fun fromString(string: String?): Point? {
        if(string.isNullOrBlank()) return null //Empty strings aren't valid
        val xy = string.split(",", limit = 2) //Only using 2 coordinate values so max is 2
        if(xy.size < 2) return null //Min values is also 2
        val x = xy[0].trim().toIntOrNull() //Trim white space, try to convert
        val y = xy[1].trim().toIntOrNull()
        return if(x == null || y == null) null //If either conversion fails, count as invalid
        else Point(x, y)
    }

    override fun toString(point: Point?): String {
        return "${point?.x},${point?.y}"
    }
}

class MainView : View("Hello TornadoFX") {
    val point = Point(5, 6) //Probably doesn't need to be its own member
    val pointProperty = SimpleObjectProperty<Point>(point)
    val pc = PointConverter()

    override val root = vbox {
        label(pointProperty, converter = pc) //Avoid extra properties, put converter in construction
        textfield(pointProperty, pc)
    }
}

I made edits to your converter to "account" for invalid input by just returning null. This is just a simple band-aid solution that doesn't enforce correct input, but it does refuse to put bad values in your property.

Upvotes: 1

Douglas K. N.
Douglas K. N.

Reputation: 43

This can probably be done more cleanly. I bet there is a way around the extra property. The example is fragile because it doesn't do input checking in the interest of keeping it simple. But it works to demonstrate the solution:

class Point(x: Int, y: Int) {
    val x: Int = x
    val y: Int = y
}

class PointConverter: StringConverter<Point?>() {
    override fun fromString(string: String?): Point? {
        val xy = string?.split(",")
        return Point(xy[0].toInt(), xy[1].toInt())
    }

    override fun toString(point: Point?): String {
        return "${point?.x},${point?.y}"
    }
}

class MainView : View("Hello TornadoFX") {
    val point = Point(5, 6)
    val pointProperty = SimpleObjectProperty<Point>(point)
    val pointDisplayProperty = SimpleStringProperty()
    val pointStringProperty = SimpleStringProperty()
    val pc = PointConverter()

    init {
        pointDisplayProperty.set(pc.toString(pointProperty.value))
        pointStringProperty.set(pc.toString(pointProperty.value))
        pointStringProperty.addListener { observable, oldValue, newValue ->
            pointProperty.set(pc.fromString(newValue))
            pointDisplayProperty.set(pc.toString(pointProperty.value))
        }
    }

    override val root = vbox {
        label(pointDisplayProperty)
        textfield(pointStringProperty)
    }
}

Upvotes: 0

Related Questions