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

ComboBox: incorrect selection state after setItem at selectedIndex

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 8u20, 8u40
    • javafx
    • most recent ea 8u40b7

      /*
       * Created on 01.10.2014
       *
       */
      package de.swingempire.fx.scene.control.comboboxx;

      import java.util.logging.Logger;

      import javafx.application.Application;
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      import javafx.scene.Parent;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.ComboBox;
      import javafx.scene.control.SingleSelectionModel;
      import javafx.scene.layout.BorderPane;
      import javafx.scene.layout.HBox;
      import javafx.stage.Stage;

      The behaviour looks rather weird: the next item after the selectedIndex is the new selection - that's a singularity (neither the - intended - behaviour of choiceBox nor the normal behaviour of a list)

      /**
       * ComboBox: incorrect selection state after setItem at selectedIndex
       *
       * To reproduce:
       * - on startup, the index 0 is selected and shown
       * - click button that sets a new item at selectedIndex
       * - expected:
       * either: selectedIndex unchanged, selectedItem/comboValue updated to new item
       * (behaviour of ListView)
       * or: selectedIndex/selectedItem/comboValue cleared
       * (intended behaviour of ChoiceBox as per rejection of RT-19820)
       * - actual:
       * selectedIndex is incremented by 1, selectedItem/value == item at
       * incremented index
       *
       * @author Jeanette Winzenburg, Berlin
       */
      public class ComboBoxSetItemAtSelectedIndexBug extends Application {

          ObservableList<String> items = FXCollections.observableArrayList(
                  "9-item", "8-item", "7-item", "6-item",
                  "5-item", "4-item", "3-item", "2-item", "1-item");

          /**
           * @return
           */
          private Parent getContent() {
              String initialValue = items.get(0);
              ComboBox<String> box = new ComboBox<>(items);
              box.setValue(initialValue);
              Button setItem = new Button("Set item at selection");
              setItem.setOnAction(e -> {
                  SingleSelectionModel<String> model = box.getSelectionModel();
                  if (model == null) return;
                  int oldSelected = model.getSelectedIndex();
                  if (oldSelected == -1) return;
                  String newItem = box.getItems().get(oldSelected) + "xx";
                  box.getItems().set(oldSelected, newItem);
                  LOG.info("selected/item/value " + model.getSelectedIndex()
                          + "/" + model.getSelectedItem() + "/" + box.getValue());
              });
              
              HBox buttons = new HBox(setItem);
              BorderPane pane = new BorderPane(box);
              pane.setBottom(buttons);
              return pane;
          }


          @Override
          public void start(Stage primaryStage) throws Exception {
              Scene scene = new Scene(getContent());
              primaryStage.setScene(scene);
              primaryStage.show();
          }

          public static void main(String[] args) {
              launch();
          }
          
          @SuppressWarnings("unused")
          private static final Logger LOG = Logger.getLogger(ComboBoxSetItemAtSelectedIndexBug.class
                  .getName());
      }

            Unassigned Unassigned
            fastegal Jeanette Winzenburg
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Imported: