Skip to content

Commit a5e6e8c

Browse files
committed
Add documentation for new factories
1 parent 27bc1b0 commit a5e6e8c

File tree

1 file changed

+52
-9
lines changed

1 file changed

+52
-9
lines changed

README.md

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,54 @@ RxJavaFX has a lightweight set of features:
6161

6262
###Node Events
6363
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`.
64+
6465
```java
6566
Button incrementBttn = new Button("Increment");
6667

6768
Observable<ActionEvent> bttnEvents =
6869
JavaFxObservable.fromNodeEvents(incrementBttn, ActionEvent.ACTION);
6970
```
7071

72+
###Action Events
73+
Action events are common and do not only apply to `Node` types. They also emit from `MenuItem` and `ContextMenu` instances, as well as a few other types.
74+
75+
Therefore, a few overloaded factories are provided to emit `ActionEvent` items from these controls
76+
77+
#####Button ActionEvents
78+
```java
79+
Button incrementBttn = new Button("Increment");
80+
81+
Observable<ActionEvent> bttnEvents =
82+
JavaFxObservable.fromActionEvents(incrementBttn);
83+
```
84+
#####MenuItem ActionEvents
85+
```java
86+
MenuItem menuItem = new MenuItem("Select me");
87+
88+
Observable<ActionEvent> menuItemEvents =
89+
JavaFxObservable.fromActionEvents(menuItem);
90+
```
91+
92+
###Other Event Factories
93+
94+
There are also factories provided to convert events from a `Window` as well as a `Scene` into an `Observable`.
95+
96+
#####Emitting Mouse Movement Events
97+
98+
```java
99+
Observable<MouseEvent> sceneMouseMovements =
100+
JavaFxObservable.fromSceneEvents(scene, MouseEvent.MOUSE_MOVED);
101+
102+
sceneMouseMovements.subscribe(v -> System.out.println(v.getSceneX() + "," + v.getSceneY()));
103+
```
104+
105+
#####Emitting Window Hiding Events
106+
```java
107+
Observable<WindowEvent> windowHidingEvents =
108+
JavaFxObservable.fromWindowEvents(primaryStage,WindowEvent.WINDOW_HIDING);
109+
110+
windowHidingEvents.subscribe(v -> System.out.println("Hiding!"));
111+
```
71112

72113
###ObservableValue
73114
Not to be confused with the RxJava `Observable`, the JavaFX `ObservableValue` can be converted into an RxJava `Observable` that emits the initial value and all value changes.
@@ -170,9 +211,9 @@ import javafx.scene.Scene;
170211
import javafx.scene.control.*;
171212
import javafx.scene.layout.GridPane;
172213
import javafx.stage.Stage;
214+
import rx.Observable;
215+
import rx.Subscription;
173216
import rx.observables.JavaFxObservable;
174-
import rx.schedulers.JavaFxScheduler;
175-
import rx.schedulers.Schedulers;
176217
import rx.subscribers.JavaFxSubscriber;
177218

178219
public class RxJavaFXTest extends Application {
@@ -182,7 +223,7 @@ public class RxJavaFXTest extends Application {
182223
private final Binding<String> binding1;
183224

184225
private final TextField textInput;
185-
private final Label fippedTextLabel;
226+
private final Label flippedTextLabel;
186227
private final Binding<String> binding2;
187228

188229
private final Spinner<Integer> spinner;
@@ -191,13 +232,13 @@ public class RxJavaFXTest extends Application {
191232

192233
public RxJavaFXTest() {
193234

194-
//initialize increment demo
195-
//Turns button events into Binding
235+
//initialize increment
236+
//demoTurns button events into Binding
196237
incrementBttn = new Button("Increment");
197238
incrementLabel = new Label("");
198239

199240
Observable<ActionEvent> bttnEvents =
200-
JavaFxObservable.fromNodeEvents(incrementBttn, ActionEvent.ACTION);
241+
JavaFxObservable.fromActionEvents(incrementBttn);
201242

202243
binding1 = JavaFxSubscriber.toBinding(bttnEvents.map(e -> 1).scan(0,(x, y) -> x + y)
203244
.map(Object::toString));
@@ -208,7 +249,7 @@ public class RxJavaFXTest extends Application {
208249
//Schedules on computation Scheduler for text flip calculation
209250
//Then resumes on JavaFxScheduler thread to update Binding
210251
textInput = new TextField();
211-
fippedTextLabel = new Label();
252+
flippedTextLabel = new Label();
212253

213254
Observable<String> textInputs =
214255
JavaFxObservable.fromObservableValue(textInput.textProperty());
@@ -217,7 +258,7 @@ public class RxJavaFXTest extends Application {
217258
.map(s -> new StringBuilder(s).reverse().toString())
218259
.observeOn(JavaFxScheduler.getInstance()));
219260

220-
fippedTextLabel.textProperty().bind(binding2);
261+
flippedTextLabel.textProperty().bind(binding2);
221262

222263
//initialize Spinner value changes
223264
//Emits Change items containing old and new value
@@ -246,12 +287,14 @@ public class RxJavaFXTest extends Application {
246287
gridPane.add(incrementLabel,1,0);
247288

248289
gridPane.add(textInput,0,1);
249-
gridPane.add(fippedTextLabel, 1,1);
290+
gridPane.add(flippedTextLabel, 1,1);
250291

251292
gridPane.add(spinner,0,2);
252293
gridPane.add(spinnerChangesLabel,1,2);
253294

254295
Scene scene = new Scene(gridPane);
296+
297+
255298
primaryStage.setWidth(275);
256299
primaryStage.setScene(scene);
257300
primaryStage.show();

0 commit comments

Comments
 (0)