JavaFX ComboBox change value causes IndexOutOfBoundsException

In JavaFX, you cannot change the contents of an ObservableList while a change is already in progress. What is happening here is that your listeners (any of the ones you try) are being fired as part of the box.getSelctionModel().getSelectedItems() ObservableList changing. So basically, you cannot change the selection while a selection change is being processed.

Your solution is a bit unwieldy anyway. If you had another listener on the selected item (or combo box value), even if your method worked it would temporarily see the combo box with an “illegal” selection. E.g in the example above, if the user tries to select “1”, another listener would see the selection change to the disallowed value “1”, then back to “0”. Dealing with values that are not supposed to be allowed in this listener would likely make your program logic pretty complex.

A better approach, imho, is to prevent the user from selecting the disallowed values. You can do this with a cell factory that sets the disable property of the cell:

    box.setCellFactory(lv -> new ListCell<Integer>() {
        @Override
        public void updateItem(Integer item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                setText(null);
            } else {
                setText(item.toString());
                setDisable(item.intValue() != 0);
            }
        }
    });

Including the following in an external style sheet will give the user the usual visual hint that the items are not selectable:

.combo-box-popup .list-cell:disabled  {
    -fx-opacity: 0.4 ;
}

Leave a Comment