问题描述
JavaFX是否具有绑定类型,其中我提供带有键的可观察映射,并且计算值将始终在键/值对更改时更新?从地图中删除该密钥将导致 null
值,将其重新添加将追溯该值。
MapProperty ,
MapBinding
和 ObservableMapValue
,但似乎没有任何东西可以达到这个目的。 我已经设计了我自己的变种,但是想要使用一个相当安全且经过测试的版本。
你可以这样做
someObjectProperty.bind(Bindings.createObjectBinding(
() - > myObservableMap.get(key),myObservableMap);
或者,@ VGR在评论中指出
someObjectProperty.bind(Bindings。 valueAt(someObservableMap,key));
这是使用第二种方法的SSCCE:
import java.util.Arrays;
import javafx.application.Application;
i mport javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
公共类BindToObservableMap扩展Application {
private static final String [] keys = {key1,key2,key3};
@Override
public void start(Stage primaryStage){
ObservableMap< String,String> map = FXCollections.observableHashMap();
for(String k:keys)map.put(k,k.replaceAll(key,value));
GridPane grid = new GridPane();
grid.setHgap(5);
grid.setVgap(5);
grid.setPadding(new Insets(10));
for(int i = 0; i< keys.length; i ++){
grid.add(new Label(keys [i]),0,i);
Label boundLabel = new Label();
boundLabel.textProperty()。bind(Bindings.valueAt(map,keys [i]));
grid.add(boundLabel,1,i);
}
ComboBox< String> keyCombo = new ComboBox<>();
keyCombo.getItems()。setAll(keys);
TextField valueField = new TextField();
按钮更新=新按钮(更新);
EventHandler< ActionEvent> handler = e - > {
map.put(keyCombo.getValue(),valueField.getText());
valueField.clear();
keyCombo.requestFocus();
};
valueField.setOnAction(handler);
update.setOnAction(handler);
grid.addRow(keys.length,keyCombo,valueField);
grid.add(update,0,keys.length + 1);
场景场景=新场景(网格);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String [] args){
launch(args);
}
}
Does JavaFX have a binding type where i supply an observable map with a key and the computed value will always update as the key/value pair changes? Removing that key the key from the map will result to a null
value, adding it back will track that value back.
I've been looking around the JavaDoc and found MapProperty
, MapBinding
and ObservableMapValue
, but nothing seems to serve this purpose.
I've already designed my own variant, but would like to use a rather failsafe and tested version.
You can just do
someObjectProperty.bind(Bindings.createObjectBinding(
() -> myObservableMap.get(key), myObservableMap);
or, as @VGR points out in the comments
someObjectProperty.bind(Bindings.valueAt(someObservableMap, key));
Here is a SSCCE using the second approach:
import java.util.Arrays;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class BindToObservableMap extends Application {
private static final String[] keys = {"key1", "key2", "key3"};
@Override
public void start(Stage primaryStage) {
ObservableMap<String, String> map = FXCollections.observableHashMap();
for (String k : keys) map.put(k, k.replaceAll("key", "value"));
GridPane grid = new GridPane();
grid.setHgap(5);
grid.setVgap(5);
grid.setPadding(new Insets(10));
for (int i = 0 ; i < keys.length; i++) {
grid.add(new Label(keys[i]), 0, i);
Label boundLabel = new Label();
boundLabel.textProperty().bind(Bindings.valueAt(map, keys[i]));
grid.add(boundLabel, 1, i);
}
ComboBox<String> keyCombo = new ComboBox<>();
keyCombo.getItems().setAll(keys);
TextField valueField = new TextField();
Button update = new Button("Update");
EventHandler<ActionEvent> handler = e -> {
map.put(keyCombo.getValue(), valueField.getText());
valueField.clear();
keyCombo.requestFocus();
};
valueField.setOnAction(handler);
update.setOnAction(handler);
grid.addRow(keys.length, keyCombo, valueField);
grid.add(update, 0, keys.length + 1);
Scene scene = new Scene(grid);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这篇关于按键绑定到ObservableMap的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!