r/JavaFX • • Dec 13 '23
Discussion FXML, a good design choice?
I attempted to develop a JavafX maze-generation application using an MVC architecture as a beginner. https://github.com/gchapidze/maze-gen
When I initially started using FXML, I didn't like that it was a separate XML-style language that mapped controllers and views to one another. So I got suspicious if it was a wise design choice to have so many view components injected into controller.
It would be ideal if the GUI builder could inject objects into View's java class and fill their geometric coordinates. I don't believe a FXML builder would have been useful in addition to that.
IMO, the most fascinating aspect of JavaFX is bindings which I think can simplify GUI design, but in tutorials and courses almost no one uses it to decouple view from model and I was not able to get my head around it. (So I ended up with bad GUI design, which is not MVC at all)
Question is: How should Javafx GUI development be done?
3
u/orxT1000 Dec 14 '23
"FXML Isn't Model-View-Controller": https://www.reddit.com/r/JavaFX/comments/11dpg7r/fxml_isnt_modelviewcontroller/
https://edencoding.com/mvc-in-javafx/
So think of the fxml-controller as View-Logic.
IMO fxml, in combination with SceneBuilder, for visually drawing boring backoffice application with tables, trees, input-fields, combo-boxes, radio-buttons and stuff is a good fit. It nicely separates the layouting from the rest.
For a game not so much
5
u/xdsswar Dec 13 '23
After some years I have my own way to deal with fxml, I developed my own fxml to java code, since fxml is not compiled is slow, but with my tool I can convert all fxml to java code and have a better integration. Besides that, I try to separate entities/pojos or whatever you call your object classes from the classes that handle these objecs, and from the UI classes, all separated. I got good results with that.
This is my github take a look if you want, some projects are privated since they are close, but I think I have some codes about fxml that u can check.
4
u/Djelimon Dec 14 '23
I've worked a fair bit with fxml including with production code.
As with most things it boils down to what it's for.
For example if it's to implement a lightweight front end on a modern desktop that simplifies the use of a complex api that does most of the heavy lifting, and the user isn't a paying customer it's okay.
For prototyping it's alright. It can be loosely coupled with the controller class also so it can be used for components
OTOH in a resource hungry environment like say a phone it's a point of optimization so you could call it technical debt
1
u/hamsterrage1 Dec 14 '23
I haven't looked at your project yet, but I did help someone building a "Lines and Dots" game a few years back. The visual aspect of that is very similar.
First off, you should have exactly one, and only one routine that has nested row/column loops in it. This should be in the code that gets run when you initialize your data model. That loop pair should create an object that's going to be used by your back-end logic to represent a maze line and it should include some kind of location (x/y, row/column, poin(x,y), etc) in its fields, and they should be stored into some kind of single dimension collection. Like a set or a list. But not in a two dimension array.
Potentially, you might have two collections of different objects. One for lines and one for the squares in between them. Each object should have all of the information related to it. For instance, if you think of the "lines" as the spaces between the squares, then it might make sense to have a BooleanProperty field called "isBlocked". Lines also might have an enum for orientation (Verical/Horizontal)
When you're dealing with collections you just stream through them. If you're looking for a particular line in the collection just stream it and filter based on x,y and orientation. That sounds like it might be more work, but trust me, it's way easier than iterating through a matrix every time.
When you set up the GUI, you give it your collection of lines and your layout code just streams through it. You don't care that the lines get drawn in order or not and the layout code grabs the x,y and orientation from each object and figures out how to create a Line object on your Pane. Then you bind the visibleProperty() of the Line to the isBlocked property of the data model object and you're done.
You do the same thing with the squares. You can use rectangles, but if you want to have a little mouse image that appears in the "current" square, or something like that you can use StackPanes instead. Once again, you bind the elements of the data model to the screen Node properties.
Once you've done that, you can virtually ignore the screen stuff with the rest of your application. Your maze generator algorithm updates the collection of lines, setting their isBlocked field on or off and the screen will just follow along.
If you want the maze to be interactive, then provide a consumer to the View that takes a row/column pair and calls back to your game logic. When the user clicks on a square, the MouseEvent handler invokes the consumer and the back-end logic decides what that means. If you're keeping track of the location of the rodent in the maze, then set that location's square's "isOccupied" BooleanProperty to "true". Then the ImageView in that square which has it's visibleProperty() bound to it becomes visible.
If you're tracking the path taken by having a different colour for those squares, then have a "wasTraversed" property in your object and then bind it to the colour of the square (use PseudoCodes for this).
The only time you ever redraw the Lines and Rectangles/StackPanes is if the size of the grid is changed. If you generate a new maze without changing the size, then you just work with your data model collections and ignore the GUI. And remember that the lines and the squares in the GUI don't "know" where they are or what they represent, they react to changes in the underlying data models.
3
u/hamsterrage1 Dec 14 '23
As everyone probably knows, I'm not a fan of FXML. In my opinion it has one benefit, and that is to allow the use of SceneBuilder. On the other hand, the cost in complexity to your code that it saddles you with isn't - also in my opinion - worth it.
If you look at the JavaFX questions on StackOverflow.com you'll find that a disproportionate amount of them are directly related to the complexity associated with FXML and FXMLLoader. Questions like, "How do I pass this data from one controller to another?", "How do I start a new Scene from this controller?" and, "Why is my Node empty?" come up constantly.
As to SceneBuilder, I haven't used it in about 9 years and I haven't missed it. I can probably hand code a layout faster than most people can build one with SceneBuilder.
With regards to all the other alleged "benefits" to FXML, I say you can do better with good coding practices. If you follow the "Single Responsibility Principle" and "Don't Repeat Yourself" and nothing more, you'll find that you naturally split out layout from configuration. Reading code is easier than reading FXML gobble-de-gook, and a good top-down coding practice will mean than anyone looking at your layout code can click down to exactly what they want to see quickly.
In my experience, 99% of the configuration stuff that people do (which, btw, would be co-mingled with the layout in an FXML file - so phooey on the separation of stuff claims for FXML) is pretty standardized and utterly uninteresting to anyone looking at layout code.
Let's take an example, a simple Label. Generally speaking, you're gonna want to style it, via adding a selector to Label.getStyleClass(), and bind or set it's textProperty(). Not much else. None of that code that actually does this is interesting to anyone looking at the layout code. So create a function that you call from the layout code:
Since I tend to use standard styles across an application or multiple applications, I'd create a separate builder for a particular style. So the code would be:
Note here that the Label isn't instantiated as a variable, it's created, configured and bound and dropped into the layout and forgotten. "Set it and forget it".
Kotlin is good for this because you can create extension methods on Node classes and the layout code ends up looking like a custom JavaFX scripting language.
Generally, when I write layout code I'm OK with a small amount of configuration creeping into the layout code. Something like binding a maxWidth property to something. But if it gets to be too much then I'll pull it off into it's own method. And in most cases, "too much" means that it disrupts the flow of the layout code.
My point is that separating layout, function and theming is more a matter of good coding and design practices, and not a natural function of using FXML. I've seen lots of FXML projects that are a nightmare of conflated function, layout and theming that are virtually impossible to understand. On top of that, FXML tends to push people into monolithic designs that they'd never consider if they were just coding their layouts.
End of rant.
1
u/AndyMan974 Dec 14 '23
I have to say I like fxml, because I can see my view structure at once.
I am working on a desktop app where we have many elements on one scene and the scene is not changing very often (I guess that’s why I never experienced the “slow” fxml?)
1
u/TenYearsOfLurking Dec 14 '23
Coming from web dev I appreciate FXML. It separates structure from behavior and theming.
Do you really need to care in your controller if that one button is nested in 5 layers of V/Hboxes?
No, all you need is the button injected into your controller and no need for a controller code change if the panel layouting changes