This project contains properties which are able to notify registered listeners about changes.
Insert dependency into your maven pom.xml
:
<dependency>
<groupId>de.borisskert</groupId>
<artifactId>java-observable-properties</artifactId>
<version>0.1.0-17</version>
</dependency>
Simple properties are forced to contain a value. For optional values consider to use an OptionalProperty
instance.
Property<Object> property = new SimpleObjectProperty<>(new Object());
Object object = property.get();
property.set(new Object());
public class MyChangeListener implements ChangeListener<Object> {
@Override
public void onChange(ReadonlyProperty<Object> property, Object oldValue, Object newValue) {
// property: the property instance which is currently notifying this listener
// oldValue: the value before this change occurred
// newValue: the new value
}
}
ChangeListener<Object> listener = new MyChangeListener();
property.addListener(listener);
property.addListener(new ChangeListener<TestObject>() {
@Override
public void onChange(ReadonlyProperty<TestObject> property, TestObject oldValue, TestObject newValue) {
// property: the property instance which is currently notifying this listener
// oldValue: the value before this change occurred
// newValue: the new value
}
});
property.addListener((property, oldValue, newValue) -> {
// property: the property instance which is currently notifying this listener
// oldValue: the value before this change occurred
// newValue: the new value
});
property.removeListener(listener);
property.bind(anotherProperty);
property.unbind(boundProperty);
Property<String> property = new StringProperty<>("your property value");
Property<Integer> property = new IntegerProperty<>(123);
Property<Double> property = new DoubleProperty<>(1.23);
Property<Boolean> property = new BooleanProperty<>(true);
Property<Long> property = new LongProperty<>(123L);
Property<Float> property = new FloatProperty<>(1.23f);
short myShort = 123;
Property<Short> property = new ShortProperty<>(myShort);
Optional properties are able to be empty. Technically they contain a null
value.
OptionalProperty<Object> property = new SimpleOptionalProperty<>(new Object()); // this property is filled
OptionalProperty<Object> empty = new SimpleOptionalProperty<>(); // this one is empty
Optional<Object> maybeObject = property.asOptional();
OptionalProperty<String> property = new OptionalStringProperty<>("your property value");
OptionalProperty<String> empty = new OptionalStringProperty<>();
OptionalProperty<Integer> property = new OptionalIntegerProperty<>(123);
OptionalProperty<Integer> empty = new OptionalIntegerProperty<>();
OptionalProperty<Double> property = new OptionalDoubleProperty<>(1.23);
OptionalProperty<Double> empty = new OptionalDoubleProperty<>();
OptionalProperty<Boolean> property = new OptionalBooleanProperty<>(true);
OptionalProperty<Boolean> empty = new OptionalBooleanProperty<>();
OptionalProperty<Long> property = new OptionalLongProperty<>(123L);
OptionalProperty<Long> empty = new OptionalLongProperty<>();
OptionalProperty<Float> property = new OptionalFloatProperty<>(1.23f);
OptionalProperty<Float> empty = new OptionalFloatProperty<>();
short myShort = 123;
OptionalProperty<Short> property = new OptionalShortProperty<>(myShort);
OptionalProperty<Short> empty = new OptionalShortProperty<>();