I have a Combo populated using ObservableMap. 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(1, "234", "Sally Doe");
Member member3 = new Member(1, "345", "Billy 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.
Aucun commentaire:
Enregistrer un commentaire