0
votes

I'm trying to create a Combobox with JavaFX and FXML with dynamic items.

Right now I have the next code, and it's rendering the combo with a list of object identifiers values. For example: my.models.Province@1ac312.

<ComboBox fx:id="provinceCombo">
<items>
    <FXCollections fx:factory="observableArrayList" />
 </items>
</ComboBox>

I'm loading the combo in my controller like this:

@FXML
private ComboBox<Province> provinceCombo;

@Override
public void initialize(URL url, ResourceBundle rb) {
    this.provinceCombo.getItems().addAll(ProvinceService.getProvinces());
}

But of course, my combo doens't know which property of my object Province should display in the combo and which property should choose as value.

How can I define this in my FXML? I don't find anything on the internet with FXML.

Thanks!

1

1 Answers

0
votes

In your Province class, override the toString() method:

@Override
public String toString(){
    return name; //or whatever you want :)
}