Skip to content

Commit 1ef453e

Browse files
committed
readme update
1 parent 78faddf commit 1ef453e

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,53 @@ prop.addChangeListener((e)-> {
4848
});
4949
```
5050

51+
#### Binding
52+
53+
Binding `Observable`s will connect them. It is only possible between 2 `Observable`s with
54+
similar types. There are 2 types of bindings.
55+
56+
- Single-Directional binding, `o1.bind(o2)` will connect the value of observable
57+
`o1` to `o2`, such that any changes to `o2` will change the value of `o1`. However,
58+
changing the value of `o1` directly, with `setValue` will not be allowed, causing
59+
a `RuntimeException`.
60+
```Java
61+
ObservableIntProperty prop1 = ...;
62+
prop1.set(2);
63+
ObservableIntProperty prop2 = ...;
64+
prop2.set(10);
65+
66+
System.out.println(prop1.get()); // returns 2
67+
68+
prop1.bind(prop2);
69+
System.out.println(prop1.get()); // returns 10
70+
71+
prop2.set(100);
72+
System.out.println(prop1.get()); // returns 100
73+
System.out.println(prop2.get()); // returns 100
74+
75+
prop1.set(5); // throws IllegalStateException
76+
```
77+
78+
- Bi-Directional binding, `o1.bindBidirectional(o2)` will connect the value of observable
79+
`o1` to `o2`, such that any changes to `o2` will change the value of `o1`, and changes to `o1`
80+
will change `o2`.
81+
```Java
82+
ObservableIntProperty prop1 = ...;
83+
prop1.set(2);
84+
ObservableIntProperty prop2 = ...;
85+
prop2.set(10);
86+
87+
System.out.println(prop1.get()); // returns 2
88+
89+
prop1.bindBidirectional(prop2);
90+
System.out.println(prop1.get()); // returns 10
91+
92+
prop2.set(100);
93+
System.out.println(prop1.get()); // returns 100
94+
System.out.println(prop2.get()); // returns 100
95+
96+
prop1.set(5);
97+
System.out.println(prop1.get()); // returns 5
98+
System.out.println(prop2.get()); // returns 5
99+
```
100+

0 commit comments

Comments
 (0)