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

Inconsistent handling of InvalidationListener notifications

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Java 24, Windows 11

      A DESCRIPTION OF THE PROBLEM :
      The InvalidationListener of properties is called in an inconsistent way over different instances.
      Consider the following example code (EXAMPLE 1):

      ------EXAMPLE 1--------
      TextField tf = new TextField();
      HBox box = new HBox(5, new Label("Enter Text: "), tf);

      StringProperty sp = new SimpleStringProperty();

      tf.textProperty().addListener( (_,_,n) -> sp.set(n) );

      sp.addListener(_ -> System.out.println("invalid") );

      Thread.startVirtualThread(() -> {
      while(true)
      {
      try
      {
      Thread.sleep(5000);
      }
      catch (InterruptedException ex)
      {
      }

      Platform.runLater(() -> {
      System.out.println("runLater - text: " + sp.get() );
      });
      }
      });

      ------EXAMPLE 1--------

      When running this code, the "invalid" message will be printed of the console only after the first change of sp or after reading the value of sp via sp.get() (here simulated every 5 secs in the virtual thread). Even when setting the value frequently via sp.set(String), the invalidation listener will only be notified once if the value is not read.


      Now consider example 2:

      ------EXAMPLE 2--------
      TextField tf = new TextField();
      HBox box = new HBox(5, new Label("Enter Text: "), tf);

      tf.textProperty().addListener( _ -> System.out.println("invalid") );
      ------EXAMPLE 2--------

      This is basically the same: adding an InvalidationListener to a property, though the property is owned by TextField. However, the "invalid" message is printed on any change every time.


      This is inconsistent behaviour and leads to confusion. The latter behavour should be the preferred one even for example 1.
      By the way: A ChangeListener works correctly and consistent for both examples.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See Description, code is provided there.


            adev Anupam Dev
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: