r/JavaFX • • 7d ago

Help Any non-mvvmFX examples of MVVM for JavaFX?

Once again, in a bid to continue my efforts to learn the core of JavaFX (i.e. solutions without frameworks or libraries on top) I am wondering if anyone knows of some actual examples of MVVM being used in an JavaFX context, that isn't just relying on a third party library such as mvvmFX?

I want to see the concept without any of the magic of libraries / frameworks as, just like my other post about packaging, I feel something that is really missing in the JavaFX community is "from the ground up" guidance on core things, such as architecture.

So I either want to collate them into one place, or make my own resources to aid in filling this gap :)

9 Upvotes

10 comments sorted by

4

u/sombriks 6d ago

I am not sure if it's what you're looking for, but try a search asking for reactive UI examples with JavaFx.

Mo need for extra frameworks besides javafx runtime itself.

I find this article, recommended to me a few days ago, a good read: https://www.pragmaticcoding.ca/javafx/techniques/reactive-javafx/

I am doing short experiments, but my snippets for this where exploring something else, but there: https://github.com/sombriks/project-layouts-jbang/blob/main/spread-deps/src/org/example/ui/Checkout.java

I hope it helps.

3

u/darkwyrm42 6d ago

Came here to say this. MVCI is a framework designed specifically for reactive programming with JavaFX, and I can't recommend it enough. I've used it for years since stumbling across it on this forum.

1

u/lazystone 6d ago edited 6d ago

It's probably not fully correct to call it a framework though. It's a design pattern.

2

u/darkwyrm42 6d ago

Thank you! I couldn't remember the correct term so I used what felt appropriate

3

u/sedj601 7d ago

It's not MVVM, but I use MVC, as in https://stackoverflow.com/a/32343342/2423906. I use it with FXML, and it works great.

2

u/ThreeSixty404 JavaFX Dev 6d ago

It's not perfect but you can get a rough idea FeedFX
You should use at least a dependency injection framework though, to make things cleaner and more easily testable.
I personally always use Inverno, it's small but most importantly it's compile-time DI, so no black magic and no runtime performance cost

1

u/mstr_2 6d ago

JavaFX is not very well equipped for a textbook MVVM pattern. However, you can use FXML/2 (which is more like XAML) with JavaFX, which is tailored for the MVVM pattern: https://jfxcore.github.io/fxml-compiler/getting-started/mvvm.html

2

u/hamsterrage1 4d ago

I don't understand this. JavaFX strikes me as one of the best equipped platforms for textbook MVVM, particularly because of its extensive library of observable objects and bindings.

Unfortunately, textbook MVVM is rarely seen in the wild because it's a PIA to implement in any platform.  The problem is the interface between Model and ViewModel. Any attempt to create this without creating massive cross dependencies on the internal implementation of each part results in huge amounts of complexities.

The issue is that the ViewModel cannot know about domain objects, and the Model cannot know about Presentation Data.  The test is whether you can change the structure of either without potentially having to modify code in the other MVVM component.

Let's say you have a Customer domain object that contains fName and lName as simple Strings. That domain object is subject to external constraints, and the Model alone deals with it.

Now, in the ViewModel you have customerFName and customerLName, which are some kind of StringProperty.  The Model cannot see these.

How do you get Customer data from the Model to the ViewModel? You have to convert it into "presentation ready" generic data.  You'll need a getter and setter for each.

What if the Model needs to update or get the current value of the names from the Presentation Data? You'll need a getter and setter for each in the ViewModel.

You can get around the shear volume of getters and setters in both directions by using DTOs of some sort. But that has it's own baggage.

What a lot of people miss is that the purpose of MVVM IS the hard separation of the internal implementation of each part. Which means that the tedium of Model/ViewModel isolation often gets turfed.

From what I've seen, the result is that the ViewModel often ends becoming a "God" class, and all of the benefits of the framework go up in smoke.

1

u/mstr_2 7h ago

I think you're conflating two different aspects: the view-viewmodel boundary, which is an aspect of the UI framework. And then the viewmodel-model boundary, which is independent of the UI framework. JavaFX is only relevant for the first, and this is where FXML/2 finds its place. It allows you to write your UI mostly declaratively, and have rich binding and conversion support to bridge the impedance mismatch that you'll often find. For example, when you have list-like data in the view model, you might need to bind the "are there any items?" information to a "hidden or visible" state in the UI. Similarly, often you'll need to flip the direction of bools or format a string with data.

Sure, if you don't like a declarative UI format, then you can do the same thing in normal Java code. But if you like declarative UI, then FXML/2 gives you the expressiveness you'll need to skip Java code in many cases. This is precisely how MVVM was popularized by XAML.

Then there's the viewmodel-model boundary. You point out that many apps mess this up, and you're right. But I consider this to be a skill issue. While "view" and "view model" in MVVM can be explained pretty easily, the "model" aspect is not one simple well-defined thing, but basically the entire rest of your system.

In particular, the model is not just a second class next to the view model that mirrors its properties and structure. In my applications, the model maintains the application state, and view models are merely derived from that state (this is something that many apps also mess up; they often maintain application state in the view model). Application state is more than a single class. For example, it might have an event-based architecture instead of a property-based architecture, like in CQRS. If your model looks like your view model, and the main problem is that you've duplicated all of the properties, then I think you're doing something wrong.

1

u/Siedlerchr 6d ago

You can take a look at JabRef, we only have afterburner.fx https://github.com/jabref/jabref for view instantiation but viewmodels etc and models are not using a framework.