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

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

    List events = getEventsChoiceBox(getPersistenceContext());
    ObservableList

        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-13 16:18

    See this example of a JavaFX ChoiceBox control backed by Database IDs.

    The example works by defining a Choice class consisting of a database row ID and a string representation of the item to be displayed in the Choice box. The default toString method of Choice is overridden with a custom implementation that returns a string representation of the item to be displayed and not the database ID. When you add the choices to the ChoiceBox, the ChoiceBox will convert each Choice into a string for display. The displayed string value of the Choice is just the choice text rather than also including the database ID or using the default toString of Choice that would just display a meaningless object reference.

    Output of choicebox sample app:

    output of choicebox sample app

    Also consider a ComboBox for such an implementation, as it has a mechanisms built into it to abstract the values of nodes from the display of the nodes (via a CellFactory). Use of a ComboBox is however often more complex than a ChoiceBox.

提交回复
热议问题