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

ChoiceBox: Changing the data set does not clear the selected item.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • jfx20
    • javafx
    • None
    • generic
    • generic

      The items selected in the choicebox is not cleared when data items of the choice box is changed.

      When the item selected is not present in the replaced data set, the selection is not cleared. If the selected item is present in the replaced data set, the selection gets cleared.

      This can be reproduced with following source code or using monkey tester available in the link given below.
      Monkey tester: https://github.com/andy-goryachev-oracle/Test/blob/main/src/goryachev/monkey/MonkeyTesterApp.java

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

      public class ChoiceBoxSample extends Application {

          @Override
          public void start(Stage stage) throws Exception {
              ChoiceBox<String> choiceBox = new ChoiceBox<String>();

              ObservableList<String> items = FXCollections.observableArrayList();
              for(int i=0 ; i<100 ; i++) {
                  items.add("item " + (i + 1));
              }

              choiceBox.getItems().setAll(items);

              Button oneItem = new Button("One item");
              oneItem.setOnAction(event -> {
                  choiceBox.getItems().setAll("item 1");
              });

              Button twoItems = new Button("Two items");
              twoItems.setOnAction(event -> {
                  choiceBox.getItems().setAll("item 1", "item 2");
              });

              Button btnHundredItems = new Button("100 items");
              btnHundredItems.setOnAction(event -> {
                  ObservableList<String> hundredItems = FXCollections.observableArrayList();
                  for(int i=0 ; i<100 ; i++) {
                      hundredItems.add("item " + (i + 1));
                  }
                  choiceBox.getItems().setAll(hundredItems);
              });

              Scene scene = new Scene(new VBox(choiceBox, oneItem, twoItems, btnHundredItems), 400, 400);
              stage.setScene(scene);
              stage.show();
          }

          public static void main(String[] args) {
              launch(args);
          }
      }
      ---------- END SOURCE ----------

      Steps to reproduce:
      1. Run the above source code.
      2. Select an item with index greater than 2
      3. Click Two items button -> Choicebox items are changed, selection remains as it is.
      4. Select "item 1" in choicebox.
      5. Click One item button -> Choicebox items are changed, selection gets cleared.

            angorya Andy Goryachev
            kpk Karthik P K
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: