我尝试将我的Labels文本属性绑定到来自我的LanguageModel的String。我希望在我再次设置LanguageModel的语言时更新属性。
在我的Controller类中,我有这个:
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
this.patientLabel.textProperty().bind(LanguageModel.getObservableText("patientLabel"));
}
真正的问题在于LanguageModel类本身:
public class LanguageModel extends Model {
private static final String GET_ALL_LANGUAGE_ENTRIES_QUERY = "SELECT * FROM Language";
private static LanguageModel instance;
private static String language;
private List<Pair<String, String>> languageEntries;
//private static List<ObservableStringValue> observableStringValueList;
private static LanguageModel getInstance() {
if(LanguageModel.instance == null) {
LanguageModel.instance = (LanguageModel) MainApplication.context.getBean("languageModel");
}
return LanguageModel.instance;
}
public static void setLanguage(String language) {
LanguageModel.language = language;
LanguageModel.getInstance().getLanguageEntries();
}
public static String getText(String key) {
LanguageModel model = LanguageModel.getInstance();
for(Pair<String, String> pair : model.languageEntries) {
if(pair.getKey().equals(key)) return pair.getValue();
}
return null;
}
public static ObservableStringValue getObservableText(final String key) {
ObservableStringValue observableStringValue = new ObservableStringValue() {
@Override
public String get() {
return LanguageModel.getText(key);
}
@Override
public void addListener(ChangeListener<? super String> listener) {
}
@Override
public void removeListener(ChangeListener<? super String> listener) {
}
@Override
public String getValue() {
return LanguageModel.getText(key);
}
@Override
public void addListener(InvalidationListener listener) {
}
@Override
public void removeListener(InvalidationListener listener) {
}
};
//LanguageModel.observableStringValueList.add(observableStringValue);
return observableStringValue;
}
private void getLanguageEntries() {
this.languageEntries = this.template.query(GET_ALL_LANGUAGE_ENTRIES_QUERY, new Object[]{}, new LanguageMapper(LanguageModel.language));
}
}
我不知道如何在调用LanguageModel.setLanguage()方法后更新ObservableStringValues。也许甚至不需要ObservableStringValue。
编辑:也许它不是语言模型的工作来使字符串&#34;可观察&#34 ;;我想我应该在Controller类中实现它。但同样,我不知道怎么说&#34;好的,现在更新&#34;如果我认为更接近,我得出结论,如果价值发生变化,观察者应该自动知道它。所以缺少一些东西(我不知道),语言模型中的一些陈述,以及#34; feed&#34;带有新值的ObservableValue。
(抱歉我的英语不好。)