"No select item" on JavaFX combobox?

14,378

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.

Share:
14,378
Mateus Viccari
Author by

Mateus Viccari

Nothing interesting to say about me. I'm just writing this down because right now i have some spare time at work and i'm gonna win a bronze medal.

Updated on June 12, 2022

Comments

  • Mateus Viccari
    Mateus Viccari almost 2 years

    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?

  • SimY4
    SimY4 over 9 years
    I think that SO wants to give user the ability to select an empty value in combobox. If this is the case, then I recommend to create special NULL object (Object that will represent empty state of combobox) and add it to items list. For example, in your case it may be just an empty string. To avoid situations where you still can get null as selected value, you can force selection at controller initialization by calling cb.getSelectionModel().selectFirst()
  • Benjamin Gale
    Benjamin Gale over 9 years
    That still does not make sense to me. You would still have an 'empty' value displayed in your ComboBox. What does it mean to select an empty value? If the user is to interact with it then it should be appropriately labeled e.g. <<Clear Selection>>.
  • SimY4
    SimY4 over 9 years
    There's no such thing as an empty value in Combobox (at least right now). There's state of Combobox, when nothing is yet selected. But when user selects something for a first time, user cannot undo his selection to previous state. To give this ability, you should create an empty value yourself and add it to the list of possible values. And you can name it as you want: empty, no data, etc.
  • Benjamin Gale
    Benjamin Gale over 9 years
    Which is what I have already said. From my original post: "I suspect you really want to be able to clear the current selection" and from my comment above: "If the user is to interact with it then it should be appropriately labeled e.g. <<Clear Selection>>" My code example shows that you can have an 'empty' value in a combo box even if it makes no sense, which is the point I am making! :-)
  • Benjamin Gale
    Benjamin Gale over 9 years
    Strange, my system is reporting that I have Java 8 update 20 and the code I posted works fine.
  • kleopatra
    kleopatra over 9 years
    never-ever add real nulls: a) technically, selecting a null results in clearing the selection (open issue, because that's unspecified - javafx-jira.kenai.com/browse/RT-38494), so if a null is a contained item the system will get confused b) 100% ACK with Benjamin: doesn't make sense for the user to select a "null" c) what can make sense is to clear the selection: the keyboard gesture to achieve that is ctrl-space (doesn't work always, though, you might need a custom key binding) d) consider using a text prompt to improve the user experience if nothing selected
  • Tarje
    Tarje over 9 years
    Yes, that is weird. I'm not sure what it means that your system is reporting that you have Java 8 update 20. Are you sure you are actually using it to run the code? If you insert System.out.println("Java version is: " + System.getProperty("java.version")); into the code and run it, does it print "Java version is: 1.8.0_20"?
  • kleopatra
    kleopatra over 9 years
    @BenjaminGale confirmed the IOOB in 8u20 (but then, having a null doesn't make overly much sense anyway, as we agree)
  • NDY
    NDY about 9 years
    Can confirm, not possible on java 8_31 too. Adding null value results in an index out of bounds exception.
  • Manuel Mauky
    Manuel Mauky about 8 years
    I've tested your example and with JDK1.8.0.74 this throws an IndexOutOfBoundsException.
  • Kirby
    Kirby almost 3 years
    It would be great if you could elaborate a bit on your answer. Perhaps if you could shine some light on what it was in JavaFX 15.0.1 that made it work? Or perhaps some example of it working?
  • Sehtim
    Sehtim over 2 years
    Edited my post for you.
  • kleopatra
    kleopatra over 2 years
    never-ever override toString for application reasons .. instead, use a custom cell (for comboBox a StringConverter is another option) to configure the visuals
  • Sehtim
    Sehtim almost 2 years
    Thank you for the hint! Back then it was my first JavaFX project and I'm already using custom cell renderers for all components ; )