open-menu-button

My Thoughts on JavaFX

By: sova

Recently I’ve created a funny little program named Laylines Dialog editor using JavaFX and wanted to share some thoughts on it. They are all just my opinion and experience so far, if you disagree, you are free to comment the post.

When I first tried JavaFX I did not like it at all. Compared to Swing it was quite hard to learn, split into multiple barely related technologies like CSS, FXML, and Java logic, of course, and while it is a powerful approach, they have multiple drawbacks each.

I can sing praises to Swing for long, but let’s just say I just sticked to it for a while rejecting JavaFX existence. Recently, I gave JavaFX another shot because of CSS support and probably because Swing apps started to look and feel too dated for 2024.

I was pleasantly surprised;

It is still as unintuitive and confusing as I remember it, but it allows for MUCH faster development and looks better if you are willing to learn. In just a couple of months of work mostly on weekends only, I got a basic functionality of Laylines editor running.
It is much easier to manage complex elements such as Trees or GUI lists in JavaFX – they look much like Swing ones but more polished. For typical use-cases, you need barely any tweaking to get it working, especially if you use JavaFX Scene Builder, which I totally recommend using. For example, customizing TreeView to have checkboxes and employ custom behavior took around 20 lines of code.


public class ReplicaListCell extends CheckBoxListCell<Replica> {

@Override
public void updateItem(Replica item, boolean empty) {
    super.updateItem(item, empty);

    if (empty || item == null) {
        setGraphic(null);
        setText(null);
    } else {
        // force line wrap
        setMinWidth(this.getWidth());
        setMaxWidth(this.getWidth());
        setPrefWidth(this.getWidth());

        setWrapText(true);

        // replace long text
        if (item.text.length() > MAX_ITEM_TEXT_LENGTH) {
            setText( item
                     .text
                     .substring(0, MAX_ITEM_TEXT_LENGTH) + "..."
            );
        } else {
            setText(item.text);
        }
    }
}
}

Scene Builder, while somewhat clunky was a lot of help. I quickly positioned all elements using the pre-defined behavior of containers, such as VBox and ticked a couple of checkboxes here and there. There is a GUI form builder in Swing too, but JavaFX one can reflect CSS applied to a form, and since you would want to apply most of the styles with CSS for the sake of simplicity and conciseness it helps you grasp the looks of your app even before launching it.

I worked mostly without pre-designed layouts because you need to understand what your framework is capable of to estimate what you can implement easily and what you can’t. For example, this is what I initially thought Laylines would look like:

 

And what it looks like when I finished the basic features as of 06.01.2025:

 

So Scene Builder helped me with the designing process a lot.

Aside from the learning curve, one thing I also still pretty much hate about JavaFX is its error handling. Swing produces quite comprehensible error messages telling you what exactly broke and where. JavaFX makes a shitload of a stack trace where only a small portion actually makes sense. You just end up googling the error because an error message often doesn’t help at all. Especially if it is an FXML-related error.

Well, I got tired of my own yapping, so hope you’ve enjoyed the ride at least a little.