```Don't tell me what I'm doing wrong``` ``` pr...
# tornadofx
с
Copy code
Don't tell me what I'm doing wrong
Copy code
private val map = mapOf("1" to "one", "3" to "three", "9" to "nine")
    private val observableMap: ObservableMap<String, String> = FXCollections.observableMap(map)
    override val root = hbox {
        textfield() {
            bind(Bindings.valueAt(observableMap, "1"))
        }
    }

textfield - is not edited.
b
you are binding to an immutable property (ObjectBinding)
с
Copy code
private val nn = SimpleMapProperty( mutableMapOf("1" to "one", "3" to "three", "9" to "nine").toObservable())
override val root = hbox {
    textfield().bind(Bindings.valueAt(nn, "1"))
       }
it doesn't work that way either. I can't create an ObjectBinding for a SimpleMapProperty. I wanted to use the key to link a text field with the mutableMapOf value. Is it possible?
b
Copy code
class TestView() : View("Test") {
    private val map = mutableMapOf("1" to "one", "3" to "three", "9" to "nine")
    private val tfText = createPropertyThenBindMap("1", map)
    private val tfText2 = createPropertyThenBindMap("2", map)


    override val root = vbox {
        prefHeight = 1000.0
        textfield(tfText)
        // or,
        textfield {
            bind(tfText2)
        }
        val area = textarea()
        button("print map").action {
            area.text = map.entries.joinToString("\n") { "${it.key}:${it.value}" }
        }
    }

    private fun createPropertyThenBindMap(key: String, map: MutableMap<String, String>): StringProperty {
        val property = stringProperty(map[key])
        property.onChange { map[key] = it ?: "" }
        return property
    }
}
if you want the properties to also observers the Map values, then you need to add code
с
Copy code
class MainView : View("Hello TornadoFX") {
    private var map = mutableMapOf("Name" to "one", "Mail" to "three", "Password" to "nine")
    private var mapProperty:MutableList<StringProperty>  = createPropertyThenBindMap(map)
    var observableMap: ObservableMap<String, String> = FXCollections.observableMap(map)
    var observableList: ObservableList<StringProperty> = FXCollections.observableList(mapProperty)
    private fun createPropertyThenBindMap(map: MutableMap<String, String>): MutableList<StringProperty>  {
        val mapProperty = mutableListOf<StringProperty>()
        for (el in map) {
            val property = stringProperty(map[el.key])
            property.onChange { map[el.key] = it ?: "" }
              mapProperty.add(property)
         }
        return mapProperty
    }

    override val root = vbox {
        children.bind(observableList) {
            textfield(it)
            }
        prefHeight = 1000.0
        val area = textarea()
        button("print map").action {
            area.text = map.entries.joinToString("\n") { "${it.key}:${it.value}" }
        }
        button("Send").action {
            area.text = observableMap["Name"] + ":" + observableMap["Password"]
            observableMap["Name"] = "fgjfj"
        }
    }
    init {
        observableMap.addListener(MapChangeListener {
            var i=0
            for (el in map) {
                observableList[i].value = el.value
                i++
            }
        })
    }
}
Thank you very much!!