【发布时间】:2021-10-22 15:23:33
【问题描述】:
我有一个Country 对象列表,这些对象具有实例变量String countryName;。
我不知道如何用 Country 对象列表填充 ComboBox。
我尝试通过创建另一个名为
的列表来使用解决方法ObservableList<String> listCountriesString = FXCollections.observableArrayList();
并循环遍历它并将每个实例变量 countryName 添加到新列表中:
private ObservableList<Country> listCountries = FXCollections.observableArrayList();
for (Country country : listCountries) {
listCountriesString.add(country.getCountryName());
}
如何将 ComboBox 与我的 Country 对象一起使用,并且只显示国家/地区名称?
@FXML
ComboBox<Country> comboBoxCountry;
public class Country {
private int countryId;
private String countryName;
private String createDate;
private String lastUpdate;
public Country(int countryId, String countryName, String createDate, String lastUpdate) {
this.countryId = countryId;
this.countryName = countryName;
this.createDate = createDate;
this.lastUpdate = lastUpdate;
}
... getters and setters
【问题讨论】:
-
请在提问前做一些研究:阅读 api 文档,完成一些关于如何使用组合框的教程,将您学到的知识应用到您的上下文中 - 当卡住时,请返回 minimal reproducible example跨度>
-
The documentation for CombBox 解释了如何做到这一点。