dynamically populate ChoiceBox from editable column in TableView

前端 未结 1 1990
既然无缘
既然无缘 2021-01-27 15:18

Basically the question title says it all. I have a column of strings (called type) in a tableview and a corresponding column of numbers (called size),

1条回答
  •  深忆病人
    2021-01-27 15:44

    Assuming I understand the question correctly, you need to initialize your cargoList with an extractor:

    ObservableList cargoList = FXCollections.observableArrayList(cargo -> 
        new Observable[]{cargo.typeProperty(), cargo.sizeProperty()});
    

    ChoiceBox has a history of not working well with update notifications to its underlying list; I'm not sure if everything is fixed in the latest version. You might want to consider switching to a ComboBox instead.

    Update

    If you need to use a ChoiceBox, a workaround might be not to set the items directly to cargoList, but to update the items whenever cargoList changes. You can do this by making the following change in setMainApp(...):

    //  externalChoiceBox.setItems(cargoList); 
        externalChoiceBox.getItems().setAll(cargoList);
    

    and adding the following in your list change listener:

      cargoList.addListener(new ListChangeListener< CargoItem>(){
         public void onChanged(Change c){
             // Do your changes here
            System.out.println(c.getList()); 
            externalChoiceBox.getItems().setAll(cargoList);
         }});
    

    0 讨论(0)
提交回复
热议问题