这可能只是 ComboBox 未正确或完全初始化的问题。我从不使用“开箱即用”的组合框。我使用几行代码来设置它们。
以下是我的一个对话框控制器类中的 initialize() 方法的代码摘录(此 ComboBox 显示机构对象列表):
// this first line gets the data from my data source
// the ComboBox is referenced by the variable 'cbxInst'
ObservableList<Institution> ilist = Institution.getInstitutionList();
Callback<ListView<Institution>, ListCell<Institution>> cellfactory =
new Callback<ListView<Institution>, ListCell<Institution>>() {
@Override
public ListCell<Institution> call(ListView<Institution> p) {
return new InstitutionListCell();
}
};
cbxInst.setCellFactory(cellfactory);
cbxInst.setButtonCell(cellfactory.call(null));
cbxInst.setItems(ilist);
这里的关键点是:
为了完整起见,这里是创建机构 ListCell 实例的私有成员类:
private final class InstitutionListCell extends ListCell<Institution> {
@Override
protected void updateItem(Institution item, boolean empty){
super.updateItem(item, empty);
if (item != null) {
this.setText(item.getName());
} else {
this.setText(Census.FORMAT_TEXT_NULL);
}
}
}
如果您以类似的方式初始化您的 ComboBox,那么您的问题可能会得到解决。