From the Github website: EasyBind leverages lambdas to reduce boilerplate when creating custom bindings, provides a type-safe alternative to `Bindings.select*` methods (inspired by Anton Nashatyrev's feature request, planned for JavaFX 9) and adds monadic operations to ObservableValue.
Static methods
map
Creates a binding whose value is a mapping of some observable value.
ObservableStringValue str = ...;
Binding<Integer> strLen = EasyBind.map(str, String::length);
Compare to plain JavaFX:
ObservableStringValue str = ...;
IntegerBinding strLen = Bindings.createIntegerBinding(() -> str.get().length(), str);
combine
Creates a binding whose value is a combination of two or more (currently up to six) observable values.
ObservableStringValue str = ...;
ObservableValue<Integer> start = ...;
ObservableValue<Integer> end = ...;
Binding<String> subStr = EasyBind.combine(str, start, end, String::substring);
Compare to plain JavaFX:
ObservableStringValue str = ...;
ObservableIntegerValue start = ...;
ObservableIntegerValue end = ...;
StringBinding subStr = Bindings.createStringBinding(() -> str.get().substring(start.get(), end.get()), str, start, end);
select
Type-safe alternative to Bindings.select*
methods.
Binding<Boolean> bb = EasyBind.select(control.sceneProperty())
.select(s -> s.windowProperty())
.selectObject(w -> w.showingProperty());
Compare to plain JavaFX:
BooleanBinding bb = Bindings.selectBoolean(control.sceneProperty(), "window", "isShowing");
map list
Returns a mapped view of an ObservableList
.
ObservableList<String> tabIds = EasyBind.map(tabPane.getTabs(), Tab::getId);
combine list
Turns an observable list of observable values into a single observable value. The resulting observable value is updated when elements are added or removed to or from the list, as well as when element values change.
Property<Integer> a = new SimpleObjectProperty<>(5);
Property<Integer> b = new SimpleObjectProperty<>(10);
ObservableList<Property<Integer>> list = FXCollections.observableArrayList();
Binding<Integer> sum = EasyBind.combine(
list,
stream -> stream.reduce((a, b) -> a + b).orElse(0));
assert sum.getValue() == 0;
// sum responds to element additions
list.add(a);
list.add(b);
assert sum.getValue() == 15;
// sum responds to element value changes
a.setValue(20);
assert sum.getValue() == 30;
// sum responds to element removals
list.remove(a);
assert sum.getValue() == 10;
bind list
Occasionally one needs to synchronize the contents of an (observable) list with another observable list:
ObservableList<T> sourceList = ...;
List<T> targetList = ...;
EasyBind.listBind(targetList, sourceList);
subscribe to values
Often one wants to execute some code for each value of an ObservableValue
, that is for the current value and each new value. This typically results in code like this:
this.doSomething(observable.getValue());
observable.addListener((obs, oldValue, newValue) -> this.doSomething(newValue));
This can be expressed more concisely using the `subscribe` helper method:
```java
EasyBind.subscribe(observable, this::doSomething);
conditional collection membership
EasyBind.includeWhen
includes or excludes an element in/from a collection based on a boolean condition.
Say that you want to draw a graph and highlight an edge when the edge itself or either of its end vertices is hovered over. To achieve this, let's add .highlight CSS class to the edge node when either of the three is hovered over and remove it when none of them is hovered over:
BooleanBinding highlight = edge.hoverProperty()
.or(v1.hoverProperty())
.or(v2.hoverProperty());
EasyBind.includeWhen(edge.getStyleClass(), "highlight", highlight);
.highlight { -fx-stroke: green; }
Maven Artifact
<!-- https://mvnrepository.com/artifact/org.fxmisc.easybind/easybind -->
<dependency>
<groupId>org.fxmisc.easybind</groupId>
<artifactId>easybind</artifactId>
<version>1.0.3</version>
</dependency>