结合 ObservableMap 绑定
Combo with ObservableMap Binding
我有一个使用 ObservableMap<String, Member>
填充的组合。我想要的是,如果在组合中选择了一个项目(键),以便能够获得相应的对象(值)。这是我目前所拥有的
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import org.controlsfx.control.textfield.TextFields;
public class ComboWithHashMap extends Application{
private ComboBox autocompleteCombo = new ComboBox();
private ObservableMap<String, Member> list;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Combo Sample");
primaryStage.setWidth(450);
primaryStage.setHeight(550);
list = FXCollections.observableMap(getActiveMember());
autocompleteCombo.getItems().setAll(list.keySet());
//TextFields.bindAutoCompletion(autocompleteCombo.getEditor(), autocompleteCombo.getItems());
Scene scene = new Scene(new BorderPane(autocompleteCombo), 880, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public HashMap<String, Member> getActiveMember(){
HashMap<String, Member> map = new HashMap<String, Member>();
Member member1 = new Member(1, "123", "John Doe");
Member member2 = new Member(2, "234", "Sally Doe");
Member member3 = new Member(3, "345", "Billy Doe");
Member member4 = new Member(4, "567", "Mac Doe");
Member member5 = new Member(5, "789", "Win Doe");
map.put( member1.getMilkNo(), member1);
map.put( member2.getMilkNo(), member2);
map.put( member3.getMilkNo(), member3);
return map;
}
public class Member {
private int id;
private String milkNo;
private String fullName;
public Member(int id, String milkNo, String fullName) {
this.id = id;
this.milkNo = milkNo;
this.fullName = fullName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMilkNo() {
return milkNo;
}
public void setMilkNo(String milkNo) {
this.milkNo = milkNo;
}
@Override
public String toString() {
return fullName;
}
}
}
问题是目前我无法在选中某个键时获取到对应的对象(值)
获取组合框中字符串对应的对象值value property listener
。代码如图所示
autocompleteCombo.valueProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
Member member = (Member) list.get(newValue);
System.out.println(member.getId());
System.out.println(member.getMilkNo());
System.out.println(member.getFullName());
}
});
valueAt
method of Bindings
在给定映射和键的 ObservableMap
中创建绑定:
ObjectBinding<Member> value = Bindings.valueAt(list, autocompleteCombo.valueProperty());
请注意,虽然如果 ObservableMap
更改此绑定将更新,但您的 ComboBox
的项目将保持不变。如果您从地图中删除一个键,绑定将包含 null
,但是您的 ComboBox
仍将包含该键的项目...
我有一个使用 ObservableMap<String, Member>
填充的组合。我想要的是,如果在组合中选择了一个项目(键),以便能够获得相应的对象(值)。这是我目前所拥有的
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import org.controlsfx.control.textfield.TextFields;
public class ComboWithHashMap extends Application{
private ComboBox autocompleteCombo = new ComboBox();
private ObservableMap<String, Member> list;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Combo Sample");
primaryStage.setWidth(450);
primaryStage.setHeight(550);
list = FXCollections.observableMap(getActiveMember());
autocompleteCombo.getItems().setAll(list.keySet());
//TextFields.bindAutoCompletion(autocompleteCombo.getEditor(), autocompleteCombo.getItems());
Scene scene = new Scene(new BorderPane(autocompleteCombo), 880, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public HashMap<String, Member> getActiveMember(){
HashMap<String, Member> map = new HashMap<String, Member>();
Member member1 = new Member(1, "123", "John Doe");
Member member2 = new Member(2, "234", "Sally Doe");
Member member3 = new Member(3, "345", "Billy Doe");
Member member4 = new Member(4, "567", "Mac Doe");
Member member5 = new Member(5, "789", "Win Doe");
map.put( member1.getMilkNo(), member1);
map.put( member2.getMilkNo(), member2);
map.put( member3.getMilkNo(), member3);
return map;
}
public class Member {
private int id;
private String milkNo;
private String fullName;
public Member(int id, String milkNo, String fullName) {
this.id = id;
this.milkNo = milkNo;
this.fullName = fullName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMilkNo() {
return milkNo;
}
public void setMilkNo(String milkNo) {
this.milkNo = milkNo;
}
@Override
public String toString() {
return fullName;
}
}
}
问题是目前我无法在选中某个键时获取到对应的对象(值)
获取组合框中字符串对应的对象值value property listener
。代码如图所示
autocompleteCombo.valueProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
Member member = (Member) list.get(newValue);
System.out.println(member.getId());
System.out.println(member.getMilkNo());
System.out.println(member.getFullName());
}
});
valueAt
method of Bindings
在给定映射和键的 ObservableMap
中创建绑定:
ObjectBinding<Member> value = Bindings.valueAt(list, autocompleteCombo.valueProperty());
请注意,虽然如果 ObservableMap
更改此绑定将更新,但您的 ComboBox
的项目将保持不变。如果您从地图中删除一个键,绑定将包含 null
,但是您的 ComboBox
仍将包含该键的项目...