JavaFX - value of the "invalid" property

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

The JavaFX property is an object that is observable and wraps the value of a field. Therefore, its listers / observers are notified when a property is updated or becomes invalid. What does it mean?

+6
source share
2 answers

I found a good explanation here.

enter image description here

When intProperty.set (7168) is called, it fires an invalidation event to another. Upon receiving this invalidation event, otherProperty simply notes that its value is not a longer validity period. It does not immediately recalculate its value by querying intProperty for its value. 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 () several times; otherProperty still recounts the 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 the comments. and you will get this result.

 IntegerProperty [bound, invalid] 
Value

num2 becomes invalid because a new value has appeared but has not yet been updated. Because the JavaFX Doc describes this only because of lazy pricing.

JavaFX binding and property implementation support lazy evaluations, which means that when a change occurs, the value is not immediately recalculated. Recalculation occurs later if and when Then a value is requested.

If you want the value to be valid, call num2.getValue(); or num2.get(); Prior to System.out.println(num2); You will see that the property will be valid.

Note. In the above example, num2.bind(num1); and num1.set(56); both values ​​will be invalid for the num2 value, because the binding already changes the num2 value and set() also tries to change the value.

+4
source

It is all about lazy appreciation. This video conference in Devoxx 2011 helped me understand this concept.

Interesting material for you starts at ~ 5: 00.

0
source

Source: https://habr.com/ru/post/981692/


All Articles