Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX - meaning of property "invalid"

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?

like image 345
user3058565 Avatar asked Jan 28 '15 23:01

user3058565


People also ask

What is Property in JavaFX?

JavaFX properties are often used in conjunction with binding, a powerful mechanism for expressing direct relationships between variables. When objects participate in bindings, changes made to one object will automatically be reflected in another object. This can be useful in a variety of applications.

Are JavaFX properties thread safe?

The JavaFX scene graph, which represents the graphical user interface of a JavaFX application, is not thread-safe and can only be accessed and modified from the UI thread also known as the JavaFX Application thread.

What is a JavaFX property?

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?

What is the invalidpropertiesformatexception in Java?

This example explains about that exception. In the above declaration, the class InvalidPropertiesFormatException is the sub class of the IOException which handles the InvalidPropertiesFormatException. It creates a constructor called InvalidPropertiesFormatException for the specified parameter message of type string.

What is lazy evaluation in JavaFX and how does it work?

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.

How does the invalidation event work in 7168?

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.


1 Answers

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.

like image 117
Asif Mushtaq Avatar answered Oct 16 '22 00:10

Asif Mushtaq