JavaFX - meaning of property "invalid"
Asked Answered
P

2

8

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?

Pinchbeck answered 28/1, 2015 at 23:45 Comment(1)
Here is a blog on changes versus invalidations by the past lead developer of the JavaFX 2 properties and binding implementation.Choriocarcinoma
T
10

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.

Talkathon answered 16/6, 2016 at 20:50 Comment(0)
C
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.

Connaught answered 30/1, 2015 at 15:14 Comment(2)
@UnKnown Can't find the video anymore, sorry.Connaught
@Spootted No problem. I also add an answer to this question read if you like and let me know If I'm wrong. Thanks.Talkathon

© 2022 - 2024 — McMap. All rights reserved.