我有一个 I18N 实现,它通过属性绑定(bind) JavaFX UI 元素,例如:
def translateLabel(l: Label, key: String, args: Any*): Unit =
l.textProperty().bind(createStringBinding(key, args))
拥有属性绑定(bind)很简单并且效果很好。然而,我对 ComboBox 很挣扎,因为它需要一个 ObservableList(在我的例子中是字符串),而且我不知道如何将我的翻译器函数绑定(bind)到它。我对 ObservableValue
、ObservableList
和 Property
接口(interface)之间的差异感到矛盾,因为它们听起来都一样。
它有 itemsProperty()
和 valueProperty()
但是这些文档缺乏且模糊,所以我不确定它们可以在哪里使用。
我想要做的是有一个 ComboBox,其中所有元素(或至少选定/可见的元素)动态更改语言 (I18N),就好像它被绑定(bind)一样,就像属性一样。
编辑:
为了更容易理解,我当前的实现是:
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
是一个 StringProperty
,它已经绑定(bind)到 StringBinding
,并以这种方式在类实例化时进行翻译:
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 (如果组合框可编辑,则为用户输入的值)。
我建议将组合框中的数据作为键列表,并使用自定义单元格将每个单元格中的文本绑定(bind)到这些键的翻译。我不会说 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()
包含所选值的键。
如果您确实想将项目绑定(bind)到 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
andProperty
interfaces as they all sound the same.
ObservableValue<T>
:代表 T
类型的单个值可以观察(意味着代码发生变化时可以执行)。
Property<T>
: 代表可写 ObservableValue<T>
;目的是实现将有一个代表该值的实际变量。它定义了附加功能,允许其值绑定(bind)到其他 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>
;你不能改变它的值(因为这样做会违反绑定(bind):声明的不变量 product.getValue() == x.getValue() * y.getValue()
);然而它是可观察的,所以你可以绑定(bind)到它:
BooleanProperty answerCorrect = new SimpleBooleanProperty();
answerCorrect.bind(product.isEqualTo(42));
等等
安ObservableList
有点不同:它是 java.util.List
(元素的集合),您可以观察它对列表上的操作的响应。 IE。如果您将监听器添加到 ObservableList
,监听器可以确定元素是否被添加或删除等。
关于JavaFx ComboBox 绑定(bind)困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65128325/