JavaFxComboBox绑定混淆

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

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

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

它有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)
}

哪里noneOptionTextStringProperty一个已经绑定到StringBinding多数民众赞成在以这种方式类的实例翻译:

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

回答

itemsProperty()是内容,以显示在组合框弹出的列表; 它的值是一个ObservableList.

valueProperty()是选择的项目(或如果组合框是可编辑的所述用户输入的值)。

我建议将组合框中的数据作为键列表,并使用自定义单元格将每个单元格中的文本绑定到这些键的翻译。我不会说 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));

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

为了完整性:

我对ObservableValue,ObservableListPropertyinterfaces之间的区别感到矛盾,
因为它们听起来都一样。

ObservableValue<T>: 表示T可以观察的单个类型值(意味着代码在更改时可以执行)。

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

因此,例如:

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

x并且y都是Property<Number>;的实现SimpleDoubleProperty有一个double代表这个值的实际变量,你可以做一些事情,比如y.set(7);改变这个值。

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

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

等等。

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


以上是JavaFxComboBox绑定混淆的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>