Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8097244

[ComboBox, Javadoc] selected item displayed but selection model is empty

XMLWordPrintable

      I came across a situation where a non-editable, collapsed combobox would show a selected item but the selection model is empty. Obviously this is inconsistent and leads to subsequent errors when the code thinks nothing is selected while the user sees that something is selected.

      It seems as if there are multiple ways to cause this. I could isolate one of these into a test. The attached demo shows two radiobuttons and a combobox, plus a label which is always updated when a radiobutton is selected. The label shows the currently selected radiobutton and the current combobox selection. See also attached screenshot.

      Consider this sequence:
          cb.getSelectionModel().selectFirst();
          cb.getItems().setAll(FXCollections.observableArrayList("E1", "E2", "E3"));
          cb.getSelectionModel().clearSelection();

      This will result in a combobox with empty selection model but E1 selected on screen.

      The following sequence fixes it:
          cb.getSelectionModel().selectFirst();
          cb.getSelectionModel().clearSelection();
          cb.getItems().setAll(FXCollections.observableArrayList("E1", "E2", "E3"));

      It seems necessary to clear the selection before changing the items. As noted before, there must be more ways to cause this because in my real (non-demo) application I could not get away with this workaround. By the way, this seems to be the opposite of RT-24412 which is about not seeing the selection on screen which was made in code.

        1. combobox.png
          14 kB
          Werner Lehmann
        2. ComboboxSelectionTest.java
          3 kB
          Werner Lehmann
        3. RT-26079.patch
          3 kB
          Jonathan Giles

            jgiles Jonathan Giles
            wlehmann Werner Lehmann
            Votes:
            2 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: