You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.adoc
+9-6Lines changed: 9 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,10 +30,10 @@ When you develop a Java Swing GUI in practice, you face the following additional
30
30
31
31
* how to enforce the proper threading (view elements like JPanel, JButton, ... should be only accessed by AWT-EventDispatchThread)
32
32
* how to keep the view "responsive" by kicking off background actions (-> SwingWorker, new Thread(...), ExecutorService, ...)
33
-
* how to connect an ActionListener on e.g. a "Cancel" button with a just started SwingWorker and disconnect it after the SwingWorker finished
34
33
* how to combine the results from multiple asynchronous actions (multiple SwingWorker's, Thread's, Future's, ...) into one result for the view
35
34
* how to implement Undo/Redo, Validation, Exception-Handling, Timeouts, ...
36
35
* how to enforce acyclic relationships between "stuff" to get good (isolated) testability
36
+
* how to connect an ActionListener on e.g. a "Cancel" button with a just started SwingWorker and disconnect it after the SwingWorker finished
37
37
* ...
38
38
39
39
So, which GUI architecture should you choose? What are the benefits and drawbacks in general? And for your current project?
@@ -47,12 +47,12 @@ One day, I had a deep lock at https://en.wikipedia.org/wiki/Model_View_ViewModel
47
47
48
48
* using https://github.com/ReactiveX/RxJava[RxJava]'s http://reactivex.io/documentation/subject.html[Subject] objects as "listenable" value objects for the ViewModel
49
49
* https://github.com/ReactiveX/RxSwing[RxSwing] to connect the View widgets (JButton, JTextField, ...) to the ViewModel-Subjects as "data binding"
50
-
* https://github.com/ReactiveX/RxJava[RxJava] to react on changes of the ViewModel-Subjects and interact with the Model (backend)
50
+
* https://github.com/ReactiveX/RxJava[RxJava] to react on changes of the ViewModel-Subjects and interact with the Model (backend) -> fluent API for "flows" (aka streams, pipeline)
51
51
* use RxJava's http://reactivex.io/documentation/scheduler.html[Scheduler] and the RxSwing https://github.com/ReactiveX/RxSwing/blob/0.x/src/main/java/rx/schedulers/SwingScheduler.java[SwingScheduler] to do the threading
52
52
53
53
image::Java_MVVM_RxJava_basic_idea.png[]
54
54
55
-
I started with some small examples for different aspects. The solution was really nice IMO and I'd like to share it with you through this github repo.
55
+
I started with some small examples for different aspects. The examples were really nice IMO and I'd like to share it with you through this github repo.
56
56
57
57
== Implementing MVVM using RxJava and RxSwing
58
58
@@ -79,7 +79,7 @@ image::MVVM_dependencies.png[]
79
79
80
80
* Threading
81
81
** All the action between View and ViewModel happens on the `SwingScheduler`
82
-
** All the action between ViewModel and Model happens on `Schedulers.io()` (= cached thread pool)
82
+
** All the action between ViewModel and Model happens "async" on `Schedulers.io()` (= cached thread pool)
*** The ViewModel represents the structure of the View 1:1
93
-
** We use http://reactivex.io/documentation/subject.html[Subject]'s as "fields" in the ViewModel
93
+
** We use http://reactivex.io/documentation/subject.html[Rx Subject]'s as "fields" in the ViewModel
94
94
*** Each subject "field" in the ViewModel corresponds to one widget in the View
95
95
*** We use a little prefix for the "field" name like `v2vm_` or `vm2v_` to indicate the flow direction
96
96
*** We use the `BehaviorSubject` class for Subject instances, which is a stateful Subject to have the "current" state, an initial state and easy testability
97
-
** The ViewModel handles all the "complicated" interaction stuff between View and Model
97
+
** The ViewModel handles all the "complicated" interaction stuff between View and Model (threading, exception handling, flows, ...)
98
98
99
99
* Model
100
100
** The Model doesn't care about it's presentation and just offers an API
@@ -108,6 +108,9 @@ together with some additional fluent API code for "nice" DataBinding.
108
108
109
109
The examples start simple and get more and more complicated, adding additional aspects and features.
110
110
111
+
There is not a "full example" which shows all aspects at the moment, since this is just some code to figure out
112
+
how to build MVVM using RxJava and RxSwing. Every example shows just one or more aspects.
113
+
111
114
Scope: +
112
115
The *current* examples are all "everything in one process" examples: View, ViewModel and Model run in one process in the same JVM. +
113
116
Upcoming examples might include JavaFx, Android, Web and of course some kind of remoting to split "things" across multiple processes.
0 commit comments