JavaFX Property Classes - dummies

JavaFX Property Classes

By Doug Lowe

At the heart of JavaFX properties is a collection of classes that create property objects. There are a lot of them, as JavaFX provides four important classes for each of its basic data types, and property classes are provided for ten different data types. Do the math: That means there are 40 property classes. The following paragraphs describe the four classes for String properties:

  • ReadOnlyStringProperty: An abstract class that represents a read-only property whose value can be read but not modified.

  • StringProperty: Another abstract class that represents a read-write property. This class extends ReadOnlyStringProperty.

  • SimpleStringProperty: This is the class that you instantiate to create a read/write string property.

  • ReadOnlyStringWrapper: This is the class you instantiate to create a read-only string property. The use of this class is a bit confusing, so be sure to look into it carefully.

For your reference, this table lists all 40 of the classes used to create properties of the various types.

JavaFX Property Classes
Boolean Classes Long Classes
ReadOnlyBooleanProperty ReadOnlyLongProperty
BooleanProperty LongProperty
SimpleBooleanProperty SimpleLongProperty
ReadOnlyBooleanWrapper ReadOnlyLongWrapper
Double Classes Map
ReadOnlyDoubleProperty ReadOnlyMapProperty<K,V>
DoubleProperty MapProperty<K,V>
SimpleDoubleProperty SimpleMapProperty<K,V>
ReadOnlyDoubleWrapper ReadOnlyMapWrapper<K,V>
Float Classes Object Classes
ReadOnlyFloatProperty ReadOnlyObjectProperty<T>
FloatProperty ObjectProperty<T>
SimpleFloatProperty SimpleObjectProperty<T>
ReadOnlyFloatWrapper ReadOnlyObjectWrapper<T>
Integer Classes Set Classes
ReadOnlyIntegerProperty ReadOnlySetProperty<E>
IntegerProperty SetProperty<E>
SimpleIntegerProperty SimpleSetProperty<E>
ReadOnlyIntegerWrapper ReadOnlySetWrapper<E>
List Classes String Classes
ReadOnlyListProperty<E> ReadOnlyStringProperty
ListProperty<E> StringProperty
SimpleListProperty<E> SimpleStringProperty
ReadOnlyListWrapper<E> ReadOnlyStringWrapper

Note that four of the types shown — List, Map, Object, and Set — are generic. For the List and Set classes, you must specify the element type for the underlying list and set collections; for the Map type, you need to specify types for the keys and values. The Object property classes let you create properties of any type you wish, but you must specify the type so that JavaFX can enforce type safety.