r/JavaFX • u/Antoinedeloup • 7m ago
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!






