首页 > 解决方案 > 从列表属性到任意对象属性的 javafx 绑定

问题描述

我试图让一个类将一个属性绑定到另一个类的列表属性,其中第一个属性来自对列表中对象的汇总计算。下面的代码是我的生产代码的简化版本。(生产代码正在对 DateTime 对象进行汇总——下面代码的基本部分是列表和对象属性之间的绑定(这里,为了简单起见,它是一个字符串)。)

我尝试过各种各样的事情。一种方法是addListener在下面的 Summary 类中的列表中使用,但我遇到了奇怪的错误,侦听器回调对 Summary 对象进行更新。在做了一堆阅读之后,我认为摘要字符串和列表之间的绑定更合适,但我不知道如何将绑定连接到属性?

package com.example.demo.view

import javafx.beans.Observable
import javafx.beans.binding.StringBinding
import javafx.beans.property.SimpleIntegerProperty
import javafx.beans.property.SimpleListProperty
import javafx.beans.property.SimpleStringProperty
import javafx.collections.FXCollections
import tornadofx.View
import tornadofx.button
import tornadofx.label
import tornadofx.vbox

class Thing(x: Int) {
    val xProperty = SimpleIntegerProperty(x)
    val yProperty = SimpleStringProperty("xyz")
}

class Collection {
    private var things = FXCollections.observableList(mutableListOf<Thing>()) {
        arrayOf<Observable>(it.xProperty)
    }

    val thingsProperty = SimpleListProperty<Thing>(things)

    fun addThing(thing: Thing) {
        things.add(thing)
    }
}

class Summary(var collection: Collection) {
    val summaryBinding = object : StringBinding() {
        // The real code is more practical but
        // this is just a minimal example.
        override fun computeValue(): String {
            val sum = collection.thingsProperty.value
                .map { it.xProperty.value }
                .fold(0, { total, next -> total + next })
            return "There are $sum things."
        }
    }

    // How to make this property update when collection changes?
    val summaryProperty = SimpleStringProperty("There are ? things.")
}

class MainView : View() {
    val summary = Summary(Collection())

    override val root = vbox {
        label(summary.summaryProperty)
        button("Add Thing") {
            summary.collection.addThing(Thing(5))
        }
    }
}

标签: javafxkotlintornadofx

解决方案


请记住,我根据您的最小示例做出了这个答案:

class Thing(x: Int) {
    val xProperty = SimpleIntegerProperty(x)
    var x by xProperty

    val yProperty = SimpleStringProperty("xyz")
    var y by yProperty
}


class MainView : View() {
    val things = FXCollections.observableList(mutableListOf<Thing>()) {
        arrayOf<Observable>(it.xProperty)
    }

    val thingsProperty = SimpleListProperty<Thing>(things)

    val totalBinding = integerBinding(listProperty) {
        value.map { it.x }.fold(0, { total, next -> total + next })
    }

    val phraseBinding = stringBinding(totalBinding) { "There are $value things." }

    override val root = vbox {
        label(phraseBinding)
        button("Add Thing") {
            action {
                list.add(Thing(5))
            }
        }
    }
}

我删除了您的其他课程,因为根据示例我没有看到他们的原因。如果集合类比在您的实际项目中保存列表属性具有更多功能,则添加只需将其添加回来。如果没有,则没有理由为列表提供自己的类。summary 类实际上只是两个绑定(如果您不需要将总数与短语分开,则为一个)。除非您打算在多个视图中使用它们,否则我认为没有必要为它们提供自己的类。

我认为您最大的问题是您没有将按钮的操作包装在action {}. 所以你的代码只是添加了一个Thing(5)on init 并且没有设置动作。

PS这些var x by xProperty东西只有在你import tornadofx.*使用那个文件时才有效。


推荐阅读