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

ComboBox with Filtered List selected item reset to null on source list update

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 8u66
    • javafx
    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      U:\>java -version
      java version "1.8.0_66"
      Java(TM) SE Runtime Environment (build 1.8.0_66-b18)
      Java HotSpot(TM) 64-Bit Server VM (build 25.66-b18, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      When a combobox uses a FilteredList and the source list is updated, the selected item gets set to null while the display value stays unchanged. This is very misleading to the end user.

      ADDITIONAL REGRESSION INFORMATION:
      U:\>java -version
      java version "1.8.0_66"
      Java(TM) SE Runtime Environment (build 1.8.0_66-b18)
      Java HotSpot(TM) 64-Bit Server VM (build 25.66-b18, mixed mode)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the executable test case provided. Click on the buttons in the order described in the program's Javadocs.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expect the selected item to remain the same as the displayed item in the combo.
      ACTUAL -
      Selected item is set to null, the combobox still displays "One".

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javafx.application.Application;
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      import javafx.collections.transformation.FilteredList;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.ComboBox;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      /**
       * Test program to show a bug in the JavaFX combobox.
       *
       * Run the app then click on the buttons in this order:
       * 1. Show Selection
       * 2. Update List
       * 3. Show Selection <- displays "One" as expected
       * 4. Update List
       * 5. Show Selection <- displays "null" while the combobox still displays "One", that seems wrong to me.
       *
       */
      public class ComboBoxTest extends Application {
          public static void main(String[] args) {
              Application.launch(args);
          }

          @Override
          public void start(Stage primaryStage) throws Exception {
              final ObservableList<String> source = FXCollections.observableArrayList("One", "Two", "X", "Three");
              FilteredList<String> fl = new FilteredList<>(source, (s) -> {
           return !s.equals("X");
              });

              final ComboBox<String> comboBox = new ComboBox<>();
              comboBox.setItems(fl);
              comboBox.getSelectionModel().select("One");
              comboBox.getSelectionModel().selectedItemProperty().addListener((p, ov, nv) -> {
               System.out.println("The selected item changed to " + nv);
               Thread.dumpStack();
              });
           
              Button showButton = new Button("Show Selection");
              showButton.setOnAction((e) -> {
           System.out.println("Selection is: " + comboBox.getSelectionModel().getSelectedItem());
              });
           
              Button updateButton = new Button("Update List");
              updateButton.setOnAction((e) -> {
                  source.setAll("One", "Two", "X", "Three");
                  System.out.println("Updated the list.");
              });

              VBox root = new VBox(20, comboBox, showButton, updateButton);
              primaryStage.setScene(new Scene(root, 500, 200));
              primaryStage.show();
          }
      }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: