与ObservableMap绑定组合

时间:2016-06-27 03:29:34

标签: java user-interface javafx controlsfx

我使用ObservableMap<String, Member>填充了一个Combo。我想要的是如果在组合中选择了一个项目(键),以便能够获得相应的对象(值)。这就是我到目前为止所拥有的

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;
        }

    }

}

问题是到目前为止,当选择一个键时,我无法获得相应的对象(值)。

2 个答案:

答案 0 :(得分:1)

将对象值对应于组合框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());
    }
});

答案 1 :(得分:1)

valueAt method of Bindings在给定地图和密钥的S = {} U S1 U S2 U S3 U ... U Sn = (((({} U S1) U S2) U S3) U ...) U Sn 中创建绑定:

S = {} U {S1, S2, S3, ..., Sn}

请注意,如果 series: [{ type: 'spline', enableMouseTracking : true, data: chartData, point: { events: { mouseOver: function(e){ console.log("Mouse Over", this.series.xAxis.userOptions.labels); this.series.xAxis.userOptions.labels.enabled = true; }, mouseOut: function(e) { console.log("Mouse Out", this.series.xAxis.userOptions.labels); this.series.xAxis.userOptions.labels.enabled = false; } } } }] 发生更改,此绑定会更新,ObservableMap的项目将保持不变。如果您从地图中删除某个密钥,则该绑定会包含ObjectBinding<Member> value = Bindings.valueAt(list, autocompleteCombo.valueProperty()); ,但您的ObservableMap仍会包含此密钥的项目...