How do I populate a JavaFX ChoiceBox with data from the Database?

前端 未结 3 518
轮回少年
轮回少年 2021-01-13 15:35
private void initialize() {
    loadPersistenceContext();

    List events = getEventsChoiceBox(getPersistenceContext());
    ObservableList

        
3条回答
  •  没有蜡笔的小新
    2021-01-13 16:20

    Here is another simple implementation from forums.oracle.com

    Create a class for key - value

    public class KeyValuePair {
       private final String key;
       private final String value;
       public KeyValuePair(String key, String value) {
       this.key = key;
       this.value = value;
       }
    
      public String getKey()   {    return key;    }
    
      public String toString() {    return value;  }
    }
    

    Then create the ChoiceBox as:

    ChoiceBox choiceBox = new ChoiceBox();
    

    Fill the elements as;

    choiceBox .getItems().add(new KeyValuePair("1", "Active"));
    

    Hint: Retrive key-value pair from you database into an ArrayList and iterate

    To retrieve the value:

    choiceBox.getValue().getKey();  // returns the "1"
    choiceBox.getValue().toString();  // returns the "Active"
    

提交回复
热议问题