JavaFx ComboBox 绑定混乱

JavaFx ComboBox binding confusion

我有一个 I18N 实现,它通过属性绑定 JavaFX UI 元素,例如:

def translateLabel(l: Label, key: String, args: Any*): Unit =
    l.textProperty().bind(createStringBinding(key, args))

绑定 属性 很容易,而且效果很好。但是,我在使用 ComboBox 时遇到了困难,因为它需要一个 ObservableList(在我的例子中是字符串),而且我不知道如何将我的翻译器函数绑定到它。我对 ObservableValueObservableListProperty 接口之间的区别感到矛盾,因为它们听起来都一样。

它有 itemsProperty()valueProperty() 但是它们的文档缺乏且含糊不清,所以我不确定它们可以在哪里使用。

我想要做的是有一个 ComboBox,其中所有元素(或至少选定/可见的元素)动态地更改语言 (I18N),就像它被绑定一样,就像 属性。

编辑:

为了方便理解,我目前的实现是:

private def setAggregatorComboBox(a: Any): Unit = {

    val items: ObservableList[String] = FXCollections.observableArrayList(
        noneOptionText.getValue,
        "COUNT()",
        "AVG()",
        "SUM()"
    )

    measureAggregatorComboBox.getItems.clear()

    measureAggregatorComboBox.getItems.addAll(items)
}

其中 noneOptionText 是已经绑定到 StringBindingStringProperty,在 class 实例化时以这种方式转换:

def translateString(sp: StringProperty, key: String, args: Any*): Unit =
        sp.bind(createStringBinding(key, args))

itemsProperty()就是list of items to show in the combo box popup;它的值是 ObservableList.

valueProperty()selected item(如果组合框可编辑,则为用户输入的值)。

我建议将组合框中的 data 作为键列表,并使用自定义单元格将每个单元格中的文本绑定到翻译那些钥匙。我不会说 scala,但在 Java 中它看起来像:

ComboBox<String> comboBox = new ComboBox<>();
comboBox.getItems().setAll(getAllKeys());

class TranslationCell extends ListCell<String> {

    @Override
    protected void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        textProperty().unbind();
        if (empty || item == null) {
            setText("");
        } else {
            textProperty().bind(createStringBinding(item));
        }
    }
}

comboBox.setCellFactory(lv -> new TranslationCell());
comboBox.setButtonCell(new TranslationCell());

现在请注意,valueProperty() 包含所选值的

如果你真的想将项目绑定到 ObservableValue<ObservableList<String>> 你可以这样做:

comboBox.itemsProperty().bind(Bindings.createObjectBinding(() ->
    FXCollections.observableArrayList(...),
    ...));

其中第一个 ...String 值的可变参数,第二个 ... 是一个可观察值,其中的更改将提示重新计算列表。 (所以在你的情况下,我猜你有一个 ObservableValue<Locale> 代表当前语言环境的地方;你会用它作为第二个参数。)

在您的特定用例中(只有列表的第一个元素是国际化的),使用监听器可能更容易:

comboBox.getItems().setAll(
    noneOptionTest.getValue(), 
    "COUNT()",
    "AVG()",
    "SUM");
noneOptionTest.addListener((obs, oldVal, newVal) ->
    comboBox.getItems().set(0, newVal));

虽然我同意这稍微不那么优雅。

完整性:

I am conflicted about the difference between ObservableValue, ObservableList and Property interfaces as they all sound the same.

ObservableValue<T>:表示T类型的单个值,可以观察到(意思是代码发生变化时可以执行)。

Property<T>:表示一个可写ObservableValue<T>;目的是实现将有一个代表值的实际变量。它定义了附加功能,允许其值绑定到其他 ObservableValue<T>.

因此,例如:

DoubleProperty x = new SimpleDoubleProperty(6);
DoubleProperty y = new SimpleDoubleProperty(9);
ObservableValue<Number> product = x.multiply(y);

xy都是Property<Number>SimpleDoubleProperty 的实现有一个实际的 double 变量代表这个值,你可以做类似 y.set(7); 的事情来改变这个值。

另一方面,product 不是 Property<Number>;你不能改变它的值(因为这样做会违反绑定:声明的不变量product.getValue() == x.getValue() * y.getValue());然而它是 可观察的,因此您可以绑定到它:

BooleanProperty answerCorrect = new SimpleBooleanProperty();
answerCorrect.bind(product.isEqualTo(42));

等等

一个ObservableList有点不同:它是一个java.util.List(元素的集合),你可以观察它响应对列表的操作. IE。如果向 ObservableList 添加监听器,则监听器可以确定是否添加或删除了元素等