r/JavaFX • u/FatalCandyCorn • 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.
2
u/Scared-Ad1896 Jan 23 '21
If you are willing to consider Kotlin targeting JVM instead of pure Java, I would definitely recommend you to take a look at tornadofx. I have experience with both frameworks (mvvmfx and tornadofx), and I prefer the latter.
1
u/FatalCandyCorn Jan 23 '21
Hmmm, I've seen Kotlin before but never have written anything in it. I'm certainly never opposed to trying new languages especially something close to Java. My only question would be whether Kotlin accelerates the UI development process or will I face the same issues as in JavaFX except with a friendlier language. And I only ask that because I am already fairly familiar with the Java language. Thank you for the response!
1
u/pikob Jan 22 '21
You need a framework (or few) to handle this and separate concerns - handling views, ui logic and program services. I second mvvmFX suggestion. Take a look, study the examples there.
0
Jan 22 '21
[deleted]
2
u/FatalCandyCorn Jan 22 '21
Thank you for your response! During my degree, I was taught computer science but not much software engineering so I struggle with architecture and how to do things the correct way. That is an interesting perspective and If FXML files are not always well suited for large-scale applications, I wonder where they fit in with development. I guess I would then change my question to be what were some helpful resources for designing large-scale GUI applications using JavaFX? I'm familiar with MVC but its principles seem frustratingly vague and I would love to see some concrete examples or at the very least, a walk-through of the design process. Thanks again :)
4
u/marvk Jan 22 '21
Once your application gets beyond a certain size it becomes a maintainability nightmare
How so?
not to mention a performance hit.
Citation needed. I realize this is also anecdotal, but it takes me 1.5 seconds to load a view consisting of 23 fxml files with a total of 7000 nodes, including 8 tables and their subnodes. I kinda doubt loading that purely from code would be significantly faster.
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:includeor views loaded programatically with it'sFluentViewLoader, with data shared via Scopes and Repository classes injected into the ViewModels. I would highly recommend not using stock JavaFX for anything complicated.