Skip to content
Advertisement

“No select item” on JavaFX combobox?

What is the correct way to put an item which value is null, inside a ComboBox? I tried using myComboBox.getItems().add(null);, and it works, but whenever the user selects this value on the combo box, an exception is thrown on the console:

Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException
    at com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.subList(ReadOnlyUnbackedObservableList.java:136)

So i think maybe this is not the correct way of doing that. Any clues?

Advertisement

Answer

In my experience, this is a problem introduced in Java 8u20. In Java 8u05 and 8u11, as well as JavaFX 2.x, you could add null to the list of items, and selecting this item behaved as expected. In Java 8u20 you will get a java.lang.IndexOutOfBoundsException whenever selecting the null value.

Benjamin Gale: you will have to use Java 8u20, select an item in the ComboBox, and then select the null value to see the issue.

At the current time, the best option seems to be to create a special null-object and label it appropriately, as already mentioned.

Alternatively, if you can get by with using a ChoiceBox instead, I think you will find that it works the way you want.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement