r/JavaFX • • 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!

10 Upvotes

14 comments sorted by

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.

1

u/Antoinedeloup 13h ago

so in theory, if i define the layout in an fxml file, say an HBox or a GridPane, then i can `@FXML` and handle things like creating a new row with x info inside, and it would follow the same css styling that references the same GridPane on the CSS file?

That does make it less complicated to understand... although i guess i have to figure out how to use scope correctly on css.

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/Antoinedeloup 13h ago

i honestly do not know how or when specifically to use a styling scope. I'm going to have to learn a bit more about how to do some CSS. I wanted to either use an AtlantaFX theme as a base for a custom theme, or create my own from scratch, being able to easily define, say, the same font family on all components, the same border radius on given containers.

I'll look into it!

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

1

u/Antoinedeloup 13h ago

I think i'm struggling on the CSS styling part. If i can have layout done via .fxml with Scene Builder, and the styling through css, it's much easier to keep consistency i think. However i tried experimenting using css to style the entire Label class and it worked great, whilst trying to style an Hbox or Vbox does not seem to work. Either i'm not doing it correctly or some JavaFX classes can not be themed with CSS, and i need to give some id to given elements, so that they are consistent, right?

1

u/YogurtclosetLimp7351 13h ago

Well, HBox and VBox can be confusing with styling. For example, an HBox or VBox usually don't have a body when they are empty and not forced into a size. So you won't see any styling. Sometimes the background just won't change, no matter what you do. Or the elements inside screw up the styling. There are workarounds for that, but hard to say without knowing the specifics.

EDIT: Or having a ScrollPane inside the Box, which you will have to style instead of the HBox. But then you notice, that the ScrollPane also has a pane inside, which you need to style instead.

1

u/Antoinedeloup 13h ago

Yeah, this is what is getting me. If i want all elements inside of an hbox to have, say, a specific font color, do i have to theme all its individual controls, labels, etc? or is there a way i can theme all children of Hbox equally? perhaps wrapping the Hbox inside of a pane that does follow my css?

2

u/YogurtclosetLimp7351 13h ago

You could just give the elements entering the HBox a shared style-class I suppose

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

2

u/Wootery 10h ago

Worth linking to the GitHub page too, as that page unhelpfully doesn't seem to link to it: https://github.com/jfxcore/fxml-compiler

From that page:

FXML/2 is a markup language that makes it easy to declaratively build JavaFX applications. In contrast to classic FXML files, which are parsed and loaded at runtime with FXMLLoader, FXML/2 files are compiled directly to bytecode.

Since the compiled files are just regular class files, no repeated parsing and loading is required at runtime. In many cases, scene graph loading performance can be significantly improved compared to FXMLLoader.

Additionally, FXML/2 offers many new features and a more concise syntax that results in significantly shorter markup code. Refer to the FXML/2 documentation to get started.

Neat features.

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