Tom Nijs
Tom Nijs

Reputation: 135

JavaFX combobox displays wrong number of items when updating Observable list

I have a combobox whose items can change. When changing the items of said combobox to have less items then previously, the combobox still displays the same amount of item fields, the 'extra' fields being blank.

Like so: https://i.sstatic.net/OkMf5.png

Here is the code I use to change the combobox.

@FXML private ComboBox<Task> taskComboBox;
private ObservableList<TaskDTO> tasks = FXCollections.observableArrayList();

public Foo() {
    taskComboBox.setItems(tasks);
}

@FXML
private void loadTaskComboBox(int i) {
    tasks.clear();
    tasks.addAll(getTasks(i));
}

Any help would be greatly appreciated.

Upvotes: 1

Views: 1051

Answers (1)

eckig
eckig

Reputation: 11134

ComboBox API:

By default, when the popup list is showing, the maximum number of rows visible is 10, but this can be changed by modifying the visibleRowCount property. If the number of items in the ComboBox is less than the value of visibleRowCount, then the items size will be used instead so that the popup list is not exceedingly long.

But on the other hand, if the number of items is shrinking, the ListView (popup content) will not shrink. But you can adjust the visibleRowCountProperty to adjust the number of visible items.

Upvotes: 2

Related Questions