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

Combobox reset selected value on edition state change

XMLWordPrintable

    • x86
    • linux

      FULL PRODUCT VERSION :
      jdk 8 update 92

      ADDITIONAL OS VERSION INFORMATION :
      Well, pure java error, so... All.

      A DESCRIPTION OF THE PROBLEM :
      Well, the following code from Combobox constructor (at line 267) troubles me :

              editableProperty().addListener(o -> {
                  // when editable changes, we reset the selection / value states
                  getSelectionModel().clearSelection();
              });

      This code resets user selection when the combobox is made editable or if we prevent edition at a certain time. But the worst part : When de-activating / re-activating edition, combo-box value is null, but editor displays the value entered before de-activating the box.

      I understand that when a combobox is editable, and the value is not part of combobox items, we have to clear selection. But it should not happen if written value is in it. For me, it should be replaced with the following :

      editableProperty().addListener((obs, oldState, newState) -> {
          /*
           * when edition is disabled, we check current value can be found in available items. If not, clear
           * selection as input object is not allowed here.
           */
          if (!newValue) {
              if (getValue() != null && getItems() != null && !getItems().contains(getValue())) {
                  getSelectionModel().clearSelection();
              }
      });

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a new simple application. Put :

      - A combobox with a limited set of string objects as items.
      - A toggle button. Bind above combobox editable property on the toggle selected property.

      Set the combobox editable. Select an object using popup list. Click the toggle button. The combobox selection disappeared.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The selection should have persisted. Selection should be cleared only when typing a value which cannot be found in combo-box associated popup menu.
      ACTUAL -
      The combobox selection is cleared, whatever the editor value.

      REPRODUCIBILITY :
      This bug can be reproduced often.

      ---------- BEGIN SOURCE ----------
      package fr.geomatys.tests;

      import javafx.application.Application;
      import javafx.beans.property.SimpleStringProperty;
      import javafx.collections.FXCollections;
      import javafx.scene.Scene;
      import javafx.scene.control.ComboBox;
      import javafx.scene.control.Label;
      import javafx.scene.control.ToggleButton;
      import javafx.scene.layout.HBox;
      import javafx.stage.Stage;

      /**
       *
       * @author Alexis Manin (Geomatys)
       */
      public class TestApp extends Application {

          @Override
          public void start(Stage primaryStage) throws Exception {
              final ComboBox<String> combo = new ComboBox<>(
                      FXCollections.observableArrayList("fifi", "riri", "loulou", "Donald")
              );

              final ToggleButton button = new ToggleButton("state");
              combo.editableProperty().bind(button.selectedProperty());
              button.setSelected(true);

              final Label l = new Label();
              l.textProperty().bind(new SimpleStringProperty("Combo-box value : ").concat(combo.valueProperty().asString()));

              final HBox box = new HBox(10, combo, button, l);
              primaryStage.setResizable(true);
              primaryStage.setScene(new Scene(box));
              primaryStage.sizeToScene();
              primaryStage.show();
          }

          public static void main(final String... args) throws Exception {
              launch(args);
          }
      }
      ---------- END SOURCE ----------

            jgiles Jonathan Giles
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: