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

BooleanBinding loses its listener

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 8u20, 8u40
    • javafx
    • win7x32

      BooleanBinding loses its listener, and even same code may work or not in a bit different situations.
      Here is testing code with a gap for code that does not work:
      //both labels must change colour when (un)checking checkbox, but only first works
      package testapp;

      import javafx.application.Application;
      import javafx.beans.binding.BooleanBinding;
      import javafx.beans.property.BooleanProperty;
      import javafx.beans.value.ChangeListener;
      import javafx.scene.Scene;
      import javafx.scene.control.CheckBox;
      import javafx.scene.control.Label;
      import javafx.scene.layout.HBox;
      import javafx.scene.layout.VBox;
      import javafx.scene.paint.Color;
      import javafx.stage.Stage;

      public class TestApp extends Application {

          @Override
          public void start(Stage primaryStage) {
              Label label1 = new Label("one");
              Label label2 = new Label("two");
              CheckBox checkBox = new CheckBox();
              ChangeListener<Boolean> listener1 = (obj, oldValue, newValue) -> {
                  if (newValue) {
                      label1.setTextFill(Color.RED);
                  } else {
                      label1.setTextFill(Color.BLUE);
                  }
              };
              ChangeListener<Boolean> listener2 = (obj, oldValue, newValue) -> {
                  if (newValue) {
                      label2.setTextFill(Color.YELLOW);
                  } else {
                      label2.setTextFill(Color.GREEN);
                  }
              };
              checkBox.selectedProperty().addListener(listener1);
              //____insert_code_here_____

              //------------------------
              VBox root = new VBox(new HBox(label1, label2), checkBox);
              Scene scene = new Scene(root, 50, 50);

              primaryStage.setTitle("Hello World!");
              primaryStage.setScene(scene);
              primaryStage.show();
          }

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

      Code that does not work:
      1) just does not work:

      (checkBox.selectedProperty().not()).addListener(listener2);

      2)works, but not always:

      BooleanBinding notSelectedProperty = checkBox.selectedProperty().not();
      notSelectedProperty.addListener(listener2);

      3) i tried to make test code with "if", not with gap for copy/paste, but it stopped working:

      boolean works = true;
      if (!works) {
          (checkBox.selectedProperty().not()).addListener(listener2);//-
      } else {
          BooleanBinding notSelectedProperty = checkBox.selectedProperty().not();//+
          notSelectedProperty.addListener(listener2);//+ (this code works ONLY if you place breakpoint on this line)
      }

      Some code works as intended only with breakpoint placed, some just does not work at all.

      upd: same problems with .or(...), my app works well only with breakboints in listeners

            vadim Vadim Pakhnushev
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: