复杂的绑定情况
Complex binding situation
在 javafx 场景中我有:
以某些区域设置作为项目的组合框
包含条目(语言环境、字符串)的哈希表
用于显示和编辑文本的文本字段
我想做的是:
当locale combobox改变时,textield根据locate selected显示存储在hashtable中的文本。示例 1:如果选择了 frech,则文本字段显示 'french text'。示例 2:如果选择中文,文本字段不显示任何内容(哈希表不包含 zh 键)。
当在文本字段中输入一些文本时,哈希表会使用在组合框中选择的区域设置执行放置。示例 1:如果键入 'aaa' 并选择法语,则哈希表将条目 fr 修改为文本 'aaa'。示例 2:如果键入 'bbb' 并选择了中文,则 hastable 添加条目 (zh,'bbb')。例3:如果textfiled没有文本,选择了english,hashtable移除en entry.
最初哈希表没有空字符串,组合框总是有一个定位选择。有可能实现吗?
通过在 Hashtable
中使用 String
对象,您不能使用任何 Property
的绑定方法与其交互,但您仍然可以通过以下方式实现您的目标在这些属性上使用监听器。这是一个粗略的例子:
public class Controller {
private VBox base = new VBox();
private ComboBox<Locale> comboBox = new ComboBox<>();
private TextField textField = new TextField();
private Button button = new Button("Print");
private Hashtable<Locale, String> map = new Hashtable<>();
public VBox getBase() {
return base;
}
public Controller() {
setupUi();
addItem("Chinese");
addItem("French");
addItem("English");
comboBox.getItems().add(new Locale("Russia", "Some Russian Text"));
comboBox.getSelectionModel().selectedItemProperty()
.addListener((obs, oldVal, newVal) -> textField.setText(map.get(newVal)));
textField.textProperty().addListener((obs, oldVal, newVal) -> {
if (newVal == null || newVal.equals("")) {
map.remove(comboBox.getValue());
} else {
map.put(comboBox.getValue(), newVal);
}
});
comboBox.getSelectionModel().selectFirst();
}
private void setupUi() {
base.getChildren().addAll(comboBox, textField, button);
button.setOnAction(event -> System.out.println(map));
}
private void addItem(String name) {
Locale locale = new Locale(name, String.format("Some %s text", name));
map.put(locale, locale.text);
comboBox.getItems().add(locale);
}
}
class Locale {
String name;
String text;
Locale(String name, String text) {
this.name = name;
this.text = text;
}
@Override
public String toString() {
return name;
}
}
在 javafx 场景中我有:
以某些区域设置作为项目的组合框
包含条目(语言环境、字符串)的哈希表
用于显示和编辑文本的文本字段
我想做的是:
当locale combobox改变时,textield根据locate selected显示存储在hashtable中的文本。示例 1:如果选择了 frech,则文本字段显示 'french text'。示例 2:如果选择中文,文本字段不显示任何内容(哈希表不包含 zh 键)。
当在文本字段中输入一些文本时,哈希表会使用在组合框中选择的区域设置执行放置。示例 1:如果键入 'aaa' 并选择法语,则哈希表将条目 fr 修改为文本 'aaa'。示例 2:如果键入 'bbb' 并选择了中文,则 hastable 添加条目 (zh,'bbb')。例3:如果textfiled没有文本,选择了english,hashtable移除en entry.
最初哈希表没有空字符串,组合框总是有一个定位选择。有可能实现吗?
通过在 Hashtable
中使用 String
对象,您不能使用任何 Property
的绑定方法与其交互,但您仍然可以通过以下方式实现您的目标在这些属性上使用监听器。这是一个粗略的例子:
public class Controller {
private VBox base = new VBox();
private ComboBox<Locale> comboBox = new ComboBox<>();
private TextField textField = new TextField();
private Button button = new Button("Print");
private Hashtable<Locale, String> map = new Hashtable<>();
public VBox getBase() {
return base;
}
public Controller() {
setupUi();
addItem("Chinese");
addItem("French");
addItem("English");
comboBox.getItems().add(new Locale("Russia", "Some Russian Text"));
comboBox.getSelectionModel().selectedItemProperty()
.addListener((obs, oldVal, newVal) -> textField.setText(map.get(newVal)));
textField.textProperty().addListener((obs, oldVal, newVal) -> {
if (newVal == null || newVal.equals("")) {
map.remove(comboBox.getValue());
} else {
map.put(comboBox.getValue(), newVal);
}
});
comboBox.getSelectionModel().selectFirst();
}
private void setupUi() {
base.getChildren().addAll(comboBox, textField, button);
button.setOnAction(event -> System.out.println(map));
}
private void addItem(String name) {
Locale locale = new Locale(name, String.format("Some %s text", name));
map.put(locale, locale.text);
comboBox.getItems().add(locale);
}
}
class Locale {
String name;
String text;
Locale(String name, String text) {
this.name = name;
this.text = text;
}
@Override
public String toString() {
return name;
}
}