JavaFX: How to Create a Read/Write Property

By Doug Lowe

To create a basic JavaFX property whose value can be read and written, you need to use two of the classes for the property type: the property class of the correct type and the corresponding simple property. For example, to create a property of type Double, you need to use both the DoubleProperty class and the SimpleDoubleProperty class.

Here are the steps to create a read/write property:

  1. Create a local field for the property using the property class for the correct type.

    The field should be defined with private visibility, and it should be final. For example:

    private String Property firstName;
  2. Create an instance of the property using the simple property class of the correct type.

    The constructor for the simple property type accepts three parameters, representing the object that contains the property (usually specified as this, a string that represents the name of the property, and the property’s default value). For example:

    firstName = new SimpleStringProperty(this,
        "firstName", ");

    Here, this is specified as the containing object, firstName is the name of the property, and the default value is an empty string.

    It is often convenient to declare the private property field and instantiate the property in the same statement, like this:

    StringProperty firstName = 
        new SimpleStringProperty(this,
            "firstName", ");
  3. Create a getter for the property.

    The getter method name should be public or protected, it should be final, it should follow the property naming convention (get followed by the name of the property with an initial cap), and it should return a value of the underlying property type. It should then call the private property’s get method to retrieve the value of the property, like this:

    public final String getFirstName
    {
        return firstName.get();
    }
  4. Create a setter for the property.

    The setter method name should by public or protected, it should be final, it should follow the property naming convention (set followed by the name of the property with an initial cap), and it should accept a parameter value of the underlying property type. It should then call the private property’s set method to set the property to the passed value. For example:

    public final void setFirstName(String value)
    {
        firstName.set(value);
    }
  5. Create the property accessor.

    This method should return the property object itself:

    public final StringProperty firstNameProperty()
    {
        return firstName;
    }

    Notice that the type is StringProperty, not SimpleStringProperty.

  6. Repeat the entire procedure for every property in your class.

Here’s a complete example that implements a read/write property named firstName in a class named Customer:

public class Customer
{
    StringProperty firstName = 
        new SimpleStringProperty(this,
            "firstName", ");
    public final String getFirstName
    {
        return firstName.get();
    }
    public final void setFirstName(String value)
    {
        firstName.set(value);
    }
    public final StringProperty firstNameProperty()
    {
        return firstName;
    }
}