1

I have a Combo populated using ObservableMap<String, Member>. What I want is if an item(key) is selected in combo, to be able to get corresponding object(value). This is what I have so far

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

    }

}

The problem is so far I cannot get the corresponding object(value) when a key is selected.

fabian
  • 80,457
  • 12
  • 86
  • 114
Yunus Einsteinium
  • 1,102
  • 4
  • 21
  • 55

2 Answers2

1

Get object value corrsponding to string in combobox value property listener. The code is as shown.

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());
    }
});
Harshita Sethi
  • 2,035
  • 3
  • 24
  • 46
1

The valueAt method of Bindings creates a binding to in a ObservableMap given the map and the key:

ObjectBinding<Member> value = Bindings.valueAt(list, autocompleteCombo.valueProperty());

Note that while this binding will be updated if the ObservableMap changes, the items of your ComboBox will remain unchanged. If you remove a key from the map, the binding would contain null, however your ComboBox would still contain a item for this key...

fabian
  • 80,457
  • 12
  • 86
  • 114
  • How can i get the selected item corresponding object? – Yunus Einsteinium Jun 27 '16 at 10:51
  • [`value.get()`](https://docs.oracle.com/javase/8/javafx/api/javafx/beans/binding/ObjectBinding.html#get--) (maybe combined with a listener added to `value` to be notified of updates...) – fabian Jun 27 '16 at 11:08
  • Could use please provide the code combination? The binding on answer with listener inorder to get selected item corresponding object – Yunus Einsteinium Jun 27 '16 at 11:54
  • Unfortunately I cannot, since you didn't tell us, what you're planing to do with the `Member`. (The only "binding" in your question right now is a `AutoCompletionBinding` (comments); binding this to a single value doesn't make sense) – fabian Jun 27 '16 at 12:35
  • What i want to do is when a button is pressed, i pick the member object corresponding to selected item in combo – Yunus Einsteinium Jun 27 '16 at 12:53
  • 1
    And how does "picking" influence the application? If this is triggered by the user interacting with a different node, why use bindings at all instead of just using `list.get(autocompleteCombo.getValue())`? – fabian Jun 27 '16 at 14:39