Skip to content

Commit ed54e28

Browse files
committed
Merge remote-tracking branch 'origin/1.x' into 1.x
2 parents ec0e25b + 3423d02 commit ed54e28

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

README.md

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# RxJavaFX: JavaFX bindings for RxJava
44

5+
Read the free eBook [_Learning RxJava with JavaFX_](https://www.gitbook.com/book/thomasnield/rxjavafx-guide/details) to get started!
6+
57
Learn more about RxJava on the <a href="https://github.com/ReactiveX/RxJava/wiki">Wiki Home</a> and the <a href="http://techblog.netflix.com/2013/02/rxjava-netflix-api.html">Netflix TechBlog post</a> where RxJava was introduced.
68

79
RxJavaFX is a simple API to convert JavaFX events into RxJava Observables and vice versa. It also has a scheduler to safely move emissions to the JavaFX Event Dispatch Thread.
@@ -61,19 +63,19 @@ $ ./gradlew build
6163

6264
## Features
6365

64-
RxJavaFX has a lightweight set of features:
66+
RxJavaFX has a comprehensive set of features to interop RxJava with JavaFX:
6567
- Factories to turn `Node`, `ObservableValue`, `ObservableList`, and other component events into an RxJava `Observable`
6668
- Factories to turn an RxJava `Observable` into a JavaFX `Binding`.
6769
- A scheduler for the JavaFX dispatch thread
6870

6971
###Node Events
70-
You can get event emissions by calling `JavaFxObservable.fromNodeEvents()` and pass the JavaFX `Node` and the `EventType` you are interested in. This will return an RxJava `Observable`.
72+
You can get event emissions by calling `JavaFxObservable.eventsOf()` and pass the JavaFX `Node` and the `EventType` you are interested in. This will return an RxJava `Observable`.
7173

7274
```java
7375
Button incrementBttn = new Button("Increment");
7476

7577
Observable<ActionEvent> bttnEvents =
76-
JavaFxObservable.fromNodeEvents(incrementBttn, ActionEvent.ACTION);
78+
JavaFxObservable.eventsOf(incrementBttn, ActionEvent.ACTION);
7779
```
7880

7981
###Action Events
@@ -86,14 +88,14 @@ Therefore, a few overloaded factories are provided to emit `ActionEvent` items f
8688
Button incrementBttn = new Button("Increment");
8789

8890
Observable<ActionEvent> bttnEvents =
89-
JavaFxObservable.fromActionEvents(incrementBttn);
91+
JavaFxObservable.actionEventsOf(incrementBttn);
9092
```
9193
#####MenuItem ActionEvents
9294
```java
9395
MenuItem menuItem = new MenuItem("Select me");
9496

9597
Observable<ActionEvent> menuItemEvents =
96-
JavaFxObservable.fromActionEvents(menuItem);
98+
JavaFxObservable.actionEventsOf(menuItem);
9799
```
98100

99101
###Other Event Factories
@@ -116,15 +118,15 @@ JavaFxObservable.fromDialog(alert)
116118

117119
```java
118120
Observable<MouseEvent> sceneMouseMovements =
119-
JavaFxObservable.fromSceneEvents(scene, MouseEvent.MOUSE_MOVED);
121+
JavaFxObservable.eventsOf(scene, MouseEvent.MOUSE_MOVED);
120122

121123
sceneMouseMovements.subscribe(v -> System.out.println(v.getSceneX() + "," + v.getSceneY()));
122124
```
123125

124126
#####Emitting Window Hiding Events
125127
```java
126128
Observable<WindowEvent> windowHidingEvents =
127-
JavaFxObservable.fromWindowEvents(primaryStage,WindowEvent.WINDOW_HIDING);
129+
JavaFxObservable.eventsOf(primaryStage,WindowEvent.WINDOW_HIDING);
128130

129131
windowHidingEvents.subscribe(v -> System.out.println("Hiding!"));
130132
```
@@ -136,13 +138,13 @@ Not to be confused with the RxJava `Observable`, the JavaFX `ObservableValue` ca
136138
TextField textInput = new TextField();
137139

138140
Observable<String> textInputs =
139-
JavaFxObservable.fromObservableValue(textInput.textProperty());
141+
JavaFxObservable.valuesOf(textInput.textProperty());
140142
```
141143
Note that many Nodes in JavaFX will have an initial value, which sometimes can be `null`, and you might consider using RxJava's `skip()` operator to ignore this initial value.
142144

143145
#####ObservableValue Changes
144146

145-
For every change to an `ObservableValue`, you can emit the old value and new value as a pair. The two values will be wrapped up in a `Change` class and you can access them via `getOldVal()` and `getNewVal()`. Just call the `JavaFxObservable.fromObservableValueChanges()` factory.
147+
For every change to an `ObservableValue`, you can emit the old value and new value as a pair. The two values will be wrapped up in a `Change` class and you can access them via `getOldVal()` and `getNewVal()`. Just call the `JavaFxObservable.valuesOfChanges()` factory.
146148

147149
```java
148150
SpinnerValueFactory<Integer> svf = new SpinnerValueFactory.IntegerSpinnerValueFactory(0, 100);
@@ -151,7 +153,7 @@ spinner.setValueFactory(svf);
151153
spinner.setEditable(true);
152154

153155
Label spinnerChangesLabel = new Label();
154-
Subscription subscription = JavaFxObservable.fromObservableValueChanges(spinner.valueProperty())
156+
Subscription subscription = JavaFxObservable.changesOf(spinner.valueProperty())
155157
.map(change -> "OLD: " + change.getOldVal() + " NEW: " + change.getNewVal())
156158
.subscribe(spinnerChangesLabel::setText);
157159
```
@@ -163,21 +165,21 @@ There are several factories to emit many useful `ObservableList`, `ObservableMap
163165

164166
|Factory Method|Parameter Type|Return Type|Description|
165167
|---|---|---|---
166-
|fromObservableList()|ObservableList&lt;T>|Observable&lt;ObservableList&lt;T>>|Emits the entire `ObservableList` every time it changes|
167-
|fromObservableListAdds()|ObservableList&lt;T>|Observable&lt;T>|Emits additions to an `ObservableList`|
168-
|fromfromObservableListRemovals()|ObservableList&lt;T>|Observable&lt;T>|Emits removals from an `ObservableList`|
169-
|fromObservableListUpdates|ObservableList&lt;T>|Observable&lt;ListChange&lt;T>>|Emits every item that was the result of a change to an `ObservableList`, with an `ADDED`, `REMOVED`, or `UPDATED` flag|
170-
|fromObservableListDistinctChanges()|ObservableList&lt;T>| Observable&lt;ListChange&lt;R>>|Emits only *distinct* addtions and removals to an `ObservableList`|
171-
|fromObservableListDistinctChanges()|ObservableList&lt;T>, Func1&lt;T,R>| Observable&lt;ListChange&lt;R>>|Emits only *distinct* additions and removals to an `ObservableList` and emits the mapping|
172-
|fromObservableListDistinctChanges()|ObservableList&lt;T>, Func1&lt;T,R>| Observable&lt;ListChange&lt;R>>|Emits only *distinct* additions and removals to an `ObservableList` based on a mapping|
173-
|fromObservableMap()|ObservableMap&lt;K,T>|Observable&lt;ObservableMap&lt;K,T>>|Emits the entire `ObservableMap` every time it changes|
174-
|fromObservableMapAdds()|ObservableMap&lt;K,T>|Observable&lt;Map.Entry&lt;K,T>>|Emits every `Map.Entry<K,T>` added to an `ObservableMap`|
175-
|fromObservableMapRemovals()|ObservableMap&lt;K,T>|Observable&lt;Map.Entry&lt;K,T>>|Emits every `Map.Entry<K,T>` removed from an `ObservableMap`|
176-
|fromObservableMapChanges()|ObservableMap&lt;K,T>|Observable&lt;MapChange&lt;K,T>>|Emits every key/value pair with an `ADDED` or `REMOVED` flag.|
177-
|fromObservableSet()|ObservableSet&lt;T>|Observable&lt;ObservableSet&lt;T>>|Emits the entire `ObservableSet` every time it changes|
178-
|fromObservableSetAdds()|ObservableSet&lt;T>|Observable&lt;T>|Emits every addition to an `ObservableSet`|
179-
|fromObservableSetRemovals()|ObservableSet&lt;T>|Observable&lt;T>|Emits every removal to an `ObservableSet`|
180-
|fromObservableSetChanges()|ObservableSet&lt;T>|Observable&lt;SetChange&lt;T>|Emits every item `ADDED` or `REMOVED` item from an `ObservableSet` with the corresponding flag|
168+
|emitOnChanged()()|ObservableList&lt;T>|Observable&lt;ObservableList&lt;T>>|Emits the entire `ObservableList` every time it changes|
169+
|additionsOf()|ObservableList&lt;T>|Observable&lt;T>|Emits additions to an `ObservableList`|
170+
|removalsOf()|ObservableList&lt;T>|Observable&lt;T>|Emits removals from an `ObservableList`|
171+
|updatesOf()|ObservableList&lt;T>|Observable&lt;ListChange&lt;T>>|Emits every item that was the result of a change to an `ObservableList`, with an `ADDED`, `REMOVED`, or `UPDATED` flag|
172+
|distinctChangesOf()|ObservableList&lt;T>| Observable&lt;ListChange&lt;R>>|Emits only *distinct* addtions and removals to an `ObservableList`|
173+
|distinctMappingsOf()|ObservableList&lt;T>, Func1&lt;T,R>| Observable&lt;ListChange&lt;R>>|Emits only *distinct* additions and removals to an `ObservableList` and emits the mapping|
174+
|distinctChangesOf()|ObservableList&lt;T>, Func1&lt;T,R>| Observable&lt;ListChange&lt;R>>|Emits only *distinct* additions and removals to an `ObservableList` based on a mapping|
175+
|emitOnChanged()|ObservableMap&lt;K,T>|Observable&lt;ObservableMap&lt;K,T>>|Emits the entire `ObservableMap` every time it changes|
176+
|additionsOf()|ObservableMap&lt;K,T>|Observable&lt;Map.Entry&lt;K,T>>|Emits every `Map.Entry<K,T>` added to an `ObservableMap`|
177+
|removalsOf()|ObservableMap&lt;K,T>|Observable&lt;Map.Entry&lt;K,T>>|Emits every `Map.Entry<K,T>` removed from an `ObservableMap`|
178+
|changesOf()|ObservableMap&lt;K,T>|Observable&lt;MapChange&lt;K,T>>|Emits every key/value pair with an `ADDED` or `REMOVED` flag.|
179+
|emitOnChanged()|ObservableSet&lt;T>|Observable&lt;ObservableSet&lt;T>>|Emits the entire `ObservableSet` every time it changes|
180+
|additionsOf()|ObservableSet&lt;T>|Observable&lt;T>|Emits every addition to an `ObservableSet`|
181+
|removalsOf()|ObservableSet&lt;T>|Observable&lt;T>|Emits every removal to an `ObservableSet`|
182+
|changesOf()|ObservableSet&lt;T>|Observable&lt;SetChange&lt;T>|Emits every item `ADDED` or `REMOVED` item from an `ObservableSet` with the corresponding flag|
181183

182184

183185
###Binding
@@ -188,7 +190,7 @@ Button incrementBttn = new Button("Increment");
188190
Label incrementLabel = new Label("");
189191

190192
Observable<ActionEvent> bttnEvents =
191-
JavaFxObservable.fromNodeEvents(incrementBttn, ActionEvent.ACTION);
193+
JavaFxObservable.eventsOf(incrementBttn, ActionEvent.ACTION);
192194

193195
Observable<String> accumulations = bttnEvents.map(e -> 1)
194196
.scan(0,(x, y) -> x + y)
@@ -241,16 +243,16 @@ In UI development, it is not uncommon to have an event triggered in multiple pla
241243
```java
242244
//make refresh Button
243245
Button button = new Button("Refresh");
244-
Observable<ActionEvent> buttonClicks = JavaFxObservable.fromActionEvents(button);
246+
Observable<ActionEvent> buttonClicks = JavaFxObservable.actionEventsOf(button);
245247

246248
//make refresh MenuItem
247249
MenuItem menuItem = new MenuItem("Refresh");
248-
Observable<ActionEvent> menuItemClicks = JavaFxObservable.fromActionEvents(menuItem);
250+
Observable<ActionEvent> menuItemClicks = JavaFxObservable.actionEventsOf(menuItem);
249251

250252
//CTRL + R hotkeys on a TableView
251253
TableView<MyType> tableView = new TableView<>();
252254
Observable<ActionEvent> hotKeyPresses =
253-
JavaFxObservable.fromNodeEvents(tableView, KeyEvent.KEY_PRESSED)
255+
JavaFxObservable.eventsOf(tableView, KeyEvent.KEY_PRESSED)
254256
.filter(ke -> ke.isControlDown() && ke.getCode().equals(KeyCode.R))
255257
.map(ke -> new ActionEvent());
256258

@@ -285,21 +287,21 @@ Wherever the three controls are declared, you can `add()` the `Observable<Action
285287
```java
286288
//make refresh Button
287289
Button button = new Button("Refresh");
288-
Observable<ActionEvent> buttonClicks = JavaFxObservable.fromActionEvents(button);
290+
Observable<ActionEvent> buttonClicks = JavaFxObservable.actionEventsOf(button);
289291
myEventModel.getRefreshRequests().add(buttonClicks);
290292

291293

292294
//make refresh MenuItem
293295
MenuItem menuItem = new MenuItem("Refresh");
294-
Observable<ActionEvent> menuItemClicks = JavaFxObservable.fromActionEvents(menuItem);
296+
Observable<ActionEvent> menuItemClicks = JavaFxObservable.actionEventsOf(menuItem);
295297
myEventModel.getRefreshRequests().add(menuItemClicks);
296298

297299

298300
//CTRL + R hotkeys on a TableView
299301
TableView<MyType> tableView = new TableView<>();
300302

301303
Observable<ActionEvent> hotKeyPresses =
302-
JavaFxObservable.fromNodeEvents(tableView, KeyEvent.KEY_PRESSED)
304+
JavaFxObservable.eventsOf(tableView, KeyEvent.KEY_PRESSED)
303305
.filter(ke -> ke.isControlDown() && ke.getCode().equals(KeyCode.R))
304306
.map(ke -> new ActionEvent());
305307

@@ -309,7 +311,7 @@ myEventModel.getRefreshRequests().add(hotKeyPresses);
309311
myEventModel.getRefreshRequests().subscribe(ae -> refresh());
310312
```
311313

312-
Every time you `add()` or `remove()` an `Observable` to a `CompositeObservable`, it will affect all existing Subscribers. For UI development, this is good because there is no sensitivity to the order of adding Observables and subscribing.
314+
The `add()` method on a `CompositeObservable` returns a subscription which you can `unsubscribe()`, and this will affect all existing downstream Subscribers. For UI development, this is good because there is no sensitivity to the order of adding Observables and subscribing.
313315

314316
Of course, you can pass around any type `T` in a `CompositeObservable<T>` and not just `ActionEvent`. It can be = helpful to pass around entire data structures, such as `CompositeObservable<Set<MyType>>`, to relay requests and inputs between controls.
315317

@@ -328,7 +330,7 @@ TextField textInput = new TextField();
328330
Label fippedTextLabel = new Label();
329331

330332
Observable<String> textInputs =
331-
JavaFxObservable.fromObservableValue(textInput.textProperty());
333+
JavaFxObservable.valuesOf(textInput.textProperty());
332334

333335
sub2 = textInputs.observeOn(Schedulers.computation())
334336
.map(s -> new StringBuilder(s).reverse().toString())
@@ -391,7 +393,7 @@ public class RxJavaFXTest extends Application {
391393
incrementLabel = new Label("");
392394

393395
Observable<ActionEvent> bttnEvents =
394-
JavaFxObservable.fromActionEvents(incrementBttn);
396+
JavaFxObservable.actionEventsOf(incrementBttn);
395397

396398
binding1 = JavaFxSubscriber.toBinding(bttnEvents.map(e -> 1).scan(0,(x, y) -> x + y)
397399
.map(Object::toString));
@@ -405,7 +407,7 @@ public class RxJavaFXTest extends Application {
405407
flippedTextLabel = new Label();
406408

407409
Observable<String> textInputs =
408-
JavaFxObservable.fromObservableValue(textInput.textProperty());
410+
JavaFxObservable.valuesOf(textInput.textProperty());
409411

410412
binding2 = JavaFxSubscriber.toBinding(textInputs.observeOn(Schedulers.computation())
411413
.map(s -> new StringBuilder(s).reverse().toString())
@@ -422,7 +424,7 @@ public class RxJavaFXTest extends Application {
422424
spinner.setEditable(true);
423425

424426
spinnerChangesLabel = new Label();
425-
subscription = JavaFxObservable.fromObservableValueChanges(spinner.valueProperty())
427+
subscription = JavaFxObservable.changesOf(spinner.valueProperty())
426428
.map(change -> "OLD: " + change.getOldVal() + " NEW: " + change.getNewVal())
427429
.subscribe(spinnerChangesLabel::setText);
428430

0 commit comments

Comments
 (0)