Tuesday, 1 December 2020

JavaFX: Properties

JavaFX Property wraps a field value and its observable. So whoever want to listen to the property change, they can register to the observable. Registered listeners will get notified when a property gets updated.

 

Let’s see it with an example.

 

Employee.java

package com.sample.app.model;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Employee {

    private StringProperty firstName = new SimpleStringProperty();

    public final String getFirstName() {
        return firstName.get();
    }

    public final void setFirstName(String firstName) {
        this.firstName.set(firstName);
    }

    public StringProperty firstNameProperty() {
        return firstName;
    }

}

 

As you see above definition, Employee class has firstName property.

 

Property naming conventions

The getFirstName() method is a standard getter that returns the current value of the firstName property. By convention, this method is declared as final. Note that the return type for this method is String, not StringProperty.

 

The setFirstName(String firstName) method (also final) is a standard setter that allows a caller to set the property's value. The setter method is optional. Its parameter is also of type String.

 

Finally, the firstNameProperty() method defines the property getter. This is a new convention in which the method name contains the name of the property (firstName, in this case), followed by the word "Property." The return type is the same as the property itself (StringProperty, in this example).

 

How to listen on employee firstName property change?

Add change listener.

 

emp.firstNameProperty().addListener(new ChangeListener<String>() {

 

    @Override

    public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {

        System.out.println("Employee firstName changed from " + oldValue + " -> " + newValue);

    }

 

});

 

Find the below working application.

 

PropertyDemo.java

package com.sample.app;

import com.sample.app.model.Employee;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

public class PropertyDemo {

    public static void main(String args[]) {
        Employee emp = new Employee();

        emp.firstNameProperty().addListener(new ChangeListener<String>() {

            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                System.out.println("Employee firstName changed from " + oldValue + " -> " + newValue);
            }

        });

        emp.setFirstName("Krishna");
        emp.setFirstName("Ram");
        emp.setFirstName("Gopi");

    }

}

 

Output

Employee firstName changed from null -> Krishna

Employee firstName changed from Krishna -> Ram

Employee firstName changed from Ram -> Gopi

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment