I tried to achieve this, and I came up with a special ComboBox that disables items that I don't want to select. Below is the code for the custom class ComboBox and its use.
public class CustomComboBox<T> extends ComboBox<T> { private ArrayList<T> disabledItems = new ArrayList<T>(); public CustomComboBox() { super(); setup(); } public CustomComboBox(ObservableList<T> list) { super(list); setup(); } private void setup() { SingleSelectionModel<T> model = new SingleSelectionModel<T>() { @Override public void select(T item) { if (disabledItems.contains(item)) { return; } super.select(item); } @Override public void select(int index) { T item = getItems().get(index); if (disabledItems.contains(item)) { return; } super.select(index); } @Override protected int getItemCount() { return getItems().size(); } @Override protected T getModelItem(int index) { return getItems().get(index); } }; Callback<ListView<T>, ListCell<T>> callback = new Callback<ListView<T>, ListCell<T>>() { @Override public ListCell<T> call(ListView<T> param) { final ListCell<T> cell = new ListCell<T>() { @Override public void updateItem(T item, boolean empty) { super.updateItem(item, empty); if (item != null) { setText(item.toString()); if (disabledItems.contains(item)) { setTextFill(Color.LIGHTGRAY); setDisable(true); } } else { setText(null); } } }; return cell; } }; setSelectionModel(model); setCellFactory(callback); } public void setDisabledItems(T... items) { for (int i = 0; i < items.length; i++) { disabledItems.add(items[i]); } } }
Then add items to disable in the ComboBox:
@FXML private CustomComboBox<String> customComboBox; ... customComboBox.setDisabledItems("Item 2", "Item 3");
And change the class in the fxml file:
<views.custom.CustomComboBox ... />
source share