7

In the context of JavaFX, in what sense is a property "invalid" when it is changed? I don't understand the reason for using this term.

A JavaFX property is an object that is observable and wraps a field value. So its listerners/observers are notified when the property updates or becomes invalid. What does it mean?

Shine
  • 3,603
  • 1
  • 30
  • 54
  • 1
    Here is a [blog on changes versus invalidations](http://blog.netopyr.com/2012/02/08/when-to-use-a-changelistener-or-an-invalidationlistener/) by the past lead developer of the JavaFX 2 properties and binding implementation. – jewelsea Jan 29 '15 at 00:01

2 Answers2

9

I found good explanation here.

enter image description here

When intProperty.set(7168) is called, it fires an invalidation event to otherProperty. Upon receiving this invalidation event, otherProperty simply makes a note of the fact that its value is no longer valid. It does not immediately perform a recalculation of its value by querying intProperty for its value. The recalculation is performed later when otherProperty.get() is called. Imagine if instead of calling intProperty.set() only once as in the above code we call intProperty.set() multiple times; otherProperty still recalculates its value only once.

As after testing I found this example.

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;

public class InvalidMean{
    public static void main(String[] args){
        IntegerProperty num1 = new SimpleIntegerProperty(15);
        IntegerProperty num2 = new SimpleIntegerProperty(10);
        // num2.bind(num1);
        // num1.set(56);
        System.out.println(num2);
    }
}

Run this code You will get this output:

IntegerProperty [value: 10]

Now remove the comment from commented lines. and you will get this output.

IntegerProperty [bound, invalid]

num2's value become invalid because new value arrived but not updated yet. As JavaFX Doc describe it just because of lazy evaluation.

The JavaFX binding and property implementations all support lazy evaluation, which means that when a change occurs, the value is not immediately recomputed. Recomputation happens later, if and when the value is subsequently requested.

If you want the value should be valid call num2.getValue(); or num2.get(); Before System.out.println(num2); you will see property will be valid then.

Note: In the above example num2.bind(num1); and num1.set(56); both will make invalid the value of num2 because bind already changing the value of num2 and set() also trying to change the value.

Asif Mushtaq
  • 3,020
  • 2
  • 34
  • 64
0

It's all about lazy evaluation. This video of a conference in Devoxx 2011 helped me a lot to understand this concept.

Interesting stuff for you begins at ~5:00.

Spotted
  • 3,635
  • 14
  • 33