r/JavaFX • • Jan 22 '21

Design Patterns for multi-screen JavaFX applications

Ok, this is something that I've been thinking about a lot since I started using JavaFX. When I develop multiscreen apps in JavaFX the controller classes that go along with the FXML files always seem to blow up into god classes. I know the controller is sort of the bridge between the UI and the business logic but I was wondering if there are design patterns or different ways of keeping things a bit more modular/tidy. My controller classes usually fill up with hundreds of lines of UI configuration (listeners, UI logic, animations), and then boilerplate to call business logic. My issue with this is that things become hard to manage very quickly and I'm just not sure what the 'correct' way would look like. When I can, I try to split up complicated UIs into multiple controller/FXML files either by loading in/out screens or by developing FXML files as components and then injecting them into a bigger FXML file (This allows each component to get its own controller class). This helps but I feel like the solution is not a hundred FXML/controller files for every single component on the UI (This would certainly be a nightmare when it comes to getting the components to interact right?). I'd love to hear other people's thoughts and practices on keeping their multiscreen applications organized.

12 Upvotes

10 comments sorted by

View all comments

2

u/marvk Jan 22 '21

I would recommend using a Framework such as mvvmFX. Using such a framework coupled with a DI framework like Guice (for which mvvmFX has native support) lends itself very well to a complex UI build out of multiple views, stiched together either by using fx:include or views loaded programatically with it's FluentViewLoader, with data shared via Scopes and Repository classes injected into the ViewModels. I would highly recommend not using stock JavaFX for anything complicated.

1

u/FatalCandyCorn Jan 22 '21

Oh ok, I will definitely look into mvvmFX and FluentViewLoader, thank you!! I have been using fx:include to stitch complex components together which works wonderfully except when I need controllers to talk to each other. When I first saw fx:include my first thought was "Oh ok, that's how you do it" and then I realized how coupled your code can get when you try to get the controllers talking. Thank you again! :D

1

u/marvk Jan 22 '21

mvvmFX's Scope feature and it's ability to integrate with DI frameworks is what really helps with coupling. FluentViewLoader is "just" a way to make loading FxmlViews and JavaViews easier, including the scene graph context (like styles), resource bundles and such.

1

u/FatalCandyCorn Jan 23 '21

It seems like there are not very many youtube videos on mvvmFX unfortunately so I guess I'll read the docs on that. But thank you for the suggestions I'm really excited to learn about it and hopefully write better software! I really appreciate the feedback u/marvk!

2

u/marvk Jan 23 '21

You're welcome! Yeah, unfortunately Desktop is a niche, Java Desktop is a nice inside that niche, JavaFX is a niche inside that niche and mvvmFX is a niche inside that niche. So not too many great tutorials out there. But the documentation is pretty usable.