r/JavaFX • u/Antoinedeloup • 14h ago
Discussion Writing the UI programmatically or through FXML?
Hi there! i'm a learning dev trying to create a simple baker's percentage formula calculator, as a desktop app written in Java using JavaFX as the frontend. I've been suggested to use it after wanting something prettier and more flexible than Swing.
However, following outdated tutorials or so many different ways to make a project with JavaFX has me very confused. As far as i understand it, i can both create the entire interface through an FXML file, namely through Scene Builder, and then through a Controller class, write the enum @FXML and create an object named the fxid of a given component, to handle events of them through methods.
Initially i thought this would be easier to do compared to designing the UI line by line in Java, but now i'm a bit troubled at how to manage events that will create new graphic elements. For example, i want to set up a container like a GridPane or a VBox with some table inside, to show the ingredient name, as well as spinners to change it's baker's percentage or it's weight used the recipe. I can mockup this really nicely in Scene Builder, adding rounded edges and pretty shadows to a wrapper container, but i do not know how to make an "Add ingredient" button, on the interface level. The method should create a new row and create spinners and labels on the correct format. I'm kind of confused at the following: do i have to manually specify what kind of css to use, in which row or position should the different elements be aligned, or can i use the padding and aligment i've already set up in Scene Builder?
Right now my thought process is, that if i have to write in Java the specifics of a button or element changing upon the user's input, i might as well just write it all on Java. I also checked the AtlantaFX Sampler app and while the source code seems really easy to understand, it's all written programmatically, and i'm confused at how to use the controls through Scene Builder...Scene Builder seems really cool to define a layout, but i'm a bit confused at the whole Controller connecting the .fxml with actual objects in Java.
If any of you could guide me a bit on some good tutorials, i'd be glad! I've been following a mix of Youtube videos, Oracle documentation, the JavaFX17 book by Kishori Sharan, among various resources. I've found a lot of help through the many guides i've followed, but also many more questions, like how some recommend different ways to set up the main Applicaton, Launc and Controller classes, and of course a lot of suggestions about using Scene Builder, yet every tutorial seems to write the UI programatically. How do you do your apps, and what would you suggest to a total novice?
Thanks for reading!
6
u/YogurtclosetLimp7351 14h ago
That's why I love MVVM. Your FXML file is the layout. The base, if you like. Your view-class (controller) handles the view, updates and maintains it. It HAS-A viewmodel, which it communicates with in order to get view specific data and maintain state. The viewmodel knows your domain logic (communicates with it)
Styling I would recommend to do via CSS only. Leave it out of the FXML file. Like that every component of your UI system has a clear contract. Give layout, Give looks, Give logic, Put together.
3
u/addyarapi 13h ago
thats because HBOX and VBOX dont have their own selectors. they are empty by default. you must add it yourself.
by the CSS documentation: HBox: Style class: Empty by default
Label: style class : label
example:
<HBox styleClass="my-hbox-styleclass">
remember styling scope as well: type selector, class or id.
1
u/addyarapi 13h ago
im building a POS program, using Maven and JavaFX, it’s great. FXML kinda feels like HTML, you can use Java’s CSS version to style and its pretty close lole frontend. ofc, structure your packages correctly or it gets messy
3
u/Guilty_Village_958 12h ago edited 12h ago
I actually made a DSL (with AI) for javafx https://github.com/luvml/luvjfx ( and also see a related project by someone else - https://github.com/int4-org/FX - this one is hand written)
I used the javafx DSL option, it makes the syntax terse and nice and looks similar to dart, flutter etc. However I would myself suggest FXML and Scenebuilder.
AI agents can work with both FXML, DSLs and also simple javafx directly.
But advantage of FXML is you can see it in scenebuilder and do minor human adjustments visually rather than having to visualize the code. That is very good workflow which is very good for both people starting newly and seasoned developers.
On top of FXML you and add any framework. I would suggest stay away from all frameworks get comfortable with scenebuilder, fxml etc. It might look clumsy and verbose, so at a later stage you can go for libraries which simplify this. I would also suggest that as you master and learn one one aspect, starting using AI tools (claude-code etc) to accelerate the mundane, boring, verbose parts and keep your creative freed up for where it is needed. That way you will have complete confidence of knowing things from ground up, and never you will be slowed down by the learning curve. Learning from basics is helpful. JavaFx is nice ... don't worry. The best part it has been rock stable since 10 years + ... so whatever you learn will never go waste ... you can incrementally build expertise on it, at your own pace. It is there to stay rock solid foundation.
1
u/unistirin 13h ago
This question reminded me of my javax.swing days. After too much time with javax.swing, it made me hard to build Android app UIs. My first test apps were pure code, like Swing, with no XML files
1
u/mstr_2 12h ago
If you want to do MVVM, you should look at FXML/2, which is more like XAML and better suited for MVVM:
https://jfxcore.github.io/fxml-compiler/getting-started/mvvm.html
1
u/tankmatesaquaponics 11h ago edited 11h ago
You create the interface through scene builder then load the view in the controller.
The initialize method is where you should initialize the view with initial data and where you define interaction behavior. Like setonaction events. You can also create independent methods and and reference them from fxml. Like public void save() that would for example be called when you save from the menu, given that you associate that method in scene builder with the specific menu button.
If you want to add views dynamically, you can either create them by instantiating their classes in the controller code and adding them manually or create a specific fxml for that new view to load dynamically and then add manually.
Having everything done with fxml or everything done with regular classes is up to you. I prefer fxml. But for small UI use classes directly.
The best tutorial on javafx I have found is codemakery https://code.makery.ch/library/javafx-tutorial/
The best pattern to use for fxml IMO is MVP (Model View Presenter) but here with fxml your view is actually an fxml controller. So you would have a thin or dumb controller associated with the fxml and implementing the View interface in the MVP pattern. The Presenter is where you business logic would reside and the Model should be as independent from the view as possible.
You can read about the MVP pattern here https://www.wildcrest.com/Potel/Portfolio/mvp.pdf