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

[ComboBox] onAction event is handled 2 times, when value is set by unidirectional binding.

XMLWordPrintable

      Run attached code:

      import javafx.application.Application;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.ComboBox;
      import javafx.scene.control.TextField;
      import javafx.scene.layout.Pane;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      /**
       * @author Alexander Kirov
       */
      public class Issue4 extends Application {

          public static void main(String[] args) {
              launch(args);
          }
          final ComboBox<String> testedComboBox = new ComboBox<String>();
          static int counter = 0;

          @Override
          public void start(Stage stage) throws Exception {
              Pane pane = new Pane();
              pane.setPrefHeight(200);
              pane.setPrefWidth(200);

              testedComboBox.getItems().addAll("1", "2", "3");
              testedComboBox.setEditable(true);

              pane.getChildren().add(testedComboBox);

              VBox vb = new VBox();

              testedComboBox.setOnAction(new EventHandler<ActionEvent>() {

                  public void handle(ActionEvent t) {
                      System.out.println(counter++);
                      System.out.println(testedComboBox.getValue());
                  }
              });

              TextField tf = new TextField("Some value");
              testedComboBox.valueProperty().bind(tf.textProperty());

              vb.getChildren().addAll(pane, tf);
              Scene scene = new Scene(vb, 400, 400);
              stage.setScene(scene);
              stage.show();
          }
      }

      To reproduce:
      remove char by char string from text field.

      I see output:
      run:
      0
      Some value
      1
      Some valu
      2
      Some valu
      3
      Some val
      4
      Some val
      5
      Some va
      6
      Some va

      You can see, that listener is called 2 times each time.

            leifs Leif Samuelsson (Inactive)
            akirov Alexander Kirov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: