I have a combobox which shows list of User
objects. I have coded a custom cell factory for the combobox:
@FXML ComboBox cmbUserIds;
Just create and set a CallBack
like follows:
@FXML ComboBox cmbUserIds;
Callback, ListCell> cellFactory = new Callback, ListCell>() {
@Override
public ListCell call(ListView l) {
return new ListCell() {
@Override
protected void updateItem(User item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
setText(item.getId() + " " + item.getName());
}
}
} ;
}
}
// Just set the button cell here:
cmbUserIds.setButtonCell(cellFactory.call(null));
cmbUserIds.setCellFactory(cellFactory);