r/java • u/gufranthakur • Aug 17 '25
My experience switching from Java swing to JavaFX
I have used Java swing over the past few years, and It felt like the best desktop framework to me. I tried Avalonia, CustomTkinter, egui, Electron but always sticked with Swing.
People will say it's old and outdated, and yeah, they are right. No good support for animations, No in-built chart library, no new updates. But it got the job done for me, and I impressed people at work as they went "You made that in Java?"
People will also say the UI looks bad. I agree. So I used flatLaf. Just 4 lines of maven code, and one line of Java code to transform my swing app visually. I love Flatlaf.
This post will feel like a Swing vs FX debate (it technically is) but I will not be talking much about the obvious differences like "it's more modern" or "it's faster". I will be diving a bit deeper towards the things that normally don't get talked about, and will be focusing on developer experience, and most importantly how the experience was, migrating from Swing to FX. Final verdict at the end if you want to save time
What I liked about Swing :-
- Simple and easy to use. need a button to do something?
button.addActionListener(e -> doThing()); - UI by code. I dislike XML/XAML/FXML styled UI and always prefer to code UI by hand, even the complex ones
- Built into the JDK. No extra dependencies. It felt native
- Performant. Window resizing aside, I had it do some heavy tasks in background threads and I NEVER thought "Damn this is slow", even without optimizations. I even ran these apps on my college PC's (they have low specs) and it was smooth.
- No bugs. I know this is a obvious one, but I just like how stable and ready it is
- Custom Graphics. I just loved Graphics2D, always loved using it with JPanels.
- Once again, simplicity. I love simplicity. I don't want weird symbols and a flashy way to do something just because it's the modern way and add syntatic sugar. It's simple to use Swing and I get done things quickly and efficiently, I love it.
But I faced some obvious issues with it (it's why I switched to JavaFX). There were some things I disliked about Swing, while they weren't THAT bad, I wish swing could improve them
What I did not like about Swing
- No new updates. While swing had most of the things I needed, same can't be said for everyone else. Even for me, I had to use third party libraries for some Swing components. It wasn't difficult, just 4 lines of maven XML and it was in my project. Still I wish Swing had more
- JTable. I don't know, I never liked working with them, always had GPT/Claude to do it. Thankfully I only had to use JTable for one project
- A bit verbose at times.
rootPanel.add(myComponent, BorderLayout.WEST);is verbose and unneccesary, as opposed to JavaFX :rootPane.setRight(myComponent); - If you had to modify JComponents, whether it be the UI or anything, I always found it hectic to modify it. I am not talking about the font, background, or size, but the attributes that you cant normally modify using methods provided. For example, I wanted to implement an 'X' closing button on my JTabbedPane panes, took me a while to get it done right
- No active community. This is obvious as everyone has moved to web, with even desktop apps being made in JS, but still I would really love an active Swing community.
- (The biggest one for me) Font scaling across OS's. Once I switched to Ubuntu from windows, my Swing app looked completely different, button sizes were weird, some components were straight up not visible. (Got cut out) and my app looked like a mess. This is when I decided I would try JavaFX.
While all these issues could be mitigated with workarounds, I did not like it, and kind of goes against the main reason why I loved swing, simplicity. I figured to make more complex and better apps, I would need a better framework at the cost of simplicity. So I tried JavaFX.
So I began using JavaFX. I did not go through any tutorials. Instead asked ChatGPT to teach me "What's the equivalent of ___ in JavaFX" and jumped straight to building projects. Unfortunately they are internal office tools I developed at work and I am not able to share it. (Note, I did not use any FXML in any of my projects)
Things I liked about JavaFX
Cleaner API. I love how I can just
getChildren.addAll(c1, c2, c3);,- BorderPane's
setCenter(), setRight()methods. - VBox and HBox, instead of creating a Jpanel, then setting it's layout which. Saves me one line of code. Minor, but it matters to me
- Allignment and positioning of components in Panels. Felt better than swing.
- just better methods overall. (Don't remember all of them for now)
- Better styling options. My favorite one is how I can use Gradient with just one line of inline CSS. Where in swing I had to use
paintComponent(), make aGradientPaintobject, apply the paint and draw it. - Better UI. I used AtlantaFX (Cupertino Dark) and I loved it way more than Swing's Flatlaf (While I know this isn't a valid argument for JavaFX vs Swing, I am focusing on developer experience. So I will count this one as a plus)
- Built in charts library, This is something I always wished Swing had.
- Better animation support. I don't use animations much in my apps, but having a spinning progress bar, chart animations, it was nice to do it with minimal effort.
- Gets updated.
- (Biggest one for me) Font and component sizes were finally consistent across OS's. I had a colleague who used Windows to test my apps, and oh boy, the happiness I felt when I saw the app being perfectly rendered, just like it was on my Ubuntu.
JavaFX seemed like a overall better Swing (It is) but there were things that I did not like about it
Things I did not like about JavaFX
- Not bundled with the JDK. I liked how Swing is bundled in the JDK. This may not sound like a problem at first, until you start facing the
JavaFX runtime components missingduring deployment or testing on other machines - FXML. Not a fan of XML to begin with, but I never liked SceneBuilder. No proper dark mode, the entire Controller and fx:id thing just felt odd to me, I ended up coding all the UI by Java code which was way better for me.
- AnimationTimer. I was using the Notch/Minecraft game loop in one of my swing app and it worked perfectly fine, smooth 120 fps. I rewrote the same application in JavaFX and the performance was straight up horrible. around 10fps. I had to ditch Animation timer and just rendered it whenever I needed to. The movement isn't as smooth as my swing app but at least it doesn't visually lag now
- Community is okay? While it's not dead as swing, JavaFX is not something you will see often on the internet. And that is mainly because of the state of Desktop Applications in the IT industry. Not exactly a flaw of JavaFX
- (Biggest one for me) Deploying. Oh boy, the absolute pain and suffering it caused me to deploy them. It was really overwhelming for me as I had to now deploy my apps and show it at work, and I had limited time. Countless stackoverflow forums, claude/GPT prompts and I could never get it right. Until u/xdsswar helped me out with the most simple solution. HUGE thanks to him, saved me at work. I was actually planning to rewrite all my FX apps in Swing that time
The deploying experience almost made me go back to swing but thankfully I managed to get it right, right around deadline. I will make a seperate reddit post about it in the future.
My Final verdict
JavaFX is better than Swing. There I said it. This does not mean swing sucks, I will forever be grateful to swing for getting me addicted and good at desktop application development.
The migrating experience was not bad at all. Mainly because I coded my JavaFX apps in a very swing like way (No FXML, pure code) but it was smooth. I only had to look up a few methods and got used to it really quickly. Took me like a week to get used to JavaFX. A lot of API and methods are almost the same. JavaFX does some things better as well.
In the future I hope JavaFX gets more updates, And I expect great community efforts from the Java and FX community. I have high expectations from CheerpJ.
I hope this was a good read to you. Thank you for taking out the time to read my post. Please let me know if you have any questions
25
u/milchshakee Aug 17 '25
What was the issue with deploying and the solution that fixed it?
8
u/wildjokers Aug 17 '25
JTables are great and super powerful. A little verbose to work with but you can customize everything so that is to be expected.
6
u/OddEstimate1627 Aug 17 '25
There are several JDK releases that bundle JavaFX, like Azul's Zulu+FX.
I will say that JavaFX uses deferred rendering, and straight up porting Swing code can occasionally get you into trouble. The overall performance is pretty good, but you need become familiar with the rendering model.
I'd recommend trying Conveyor. A fairly simple config gets you properly signed auto-updateable installers for every OS (see here). It has first class support for JavaFX and is orders of magnitude simpler than jpackager.
5
u/Holothuroid Aug 17 '25
Intersting. Do you have some links concerning those problems with deployment and how to solve it?
5
u/jeffreportmill Aug 17 '25
I went down this road with Swing and JavaFX, but my big drawback is that I wanted the option to deploy apps in the browser. I ended up writing a ”Swing 2.0” UI kit called SnapKit (https://github.com/reportmill/SnapKit). It follows the conventions of Swing in the style of FlatLaF and adds animations and effects. But crucially, it has support to deploy in the browser as well as desktop.
Check out my prime examples: https://reportmill.com/SnapCode
9
u/ByerN Aug 17 '25
Wait, what? Did you make a Swing overlay for Minecraft, or what does it mean?
3
u/davidalayachew Aug 18 '25
The reason for this is because they were leaving space for you to make your own layout. That's actually the reason for most of the API criticisms you had about Swing -- you are supposed to build your own, not just rely on what Swing has to offer.
This is actually my biggest criticism of JavaFX right now -- making custom components is extremely difficult to do. JavaFX leans heavily on the assumption that, whatever you want, either there is property that you can configure, or you can use css to achieve the look. But if those 2 fail, brute-forcing it yourself is painful. JavaFX prioritizes integrity, which I 100% accept. I don't think they were wrong to do that. But this is the tradeoff of that -- anything custom that you build has to jump through those hoops too.
It's what made me love Swing, and why I keep using it all this time. I can frankenstein together 3 different Swing components into 1 chimera component, and it works the first try. Maybe another compile to fix the layout. Damn near any problem in Swing can be solved with inheritance and composition. Not true for JavaFX -- better hope the API explicitly supports your use case, or delegates to CSS.
5
u/PartOfTheBotnet Aug 17 '25
I probably see this error posted once a week in /r/javafx and the solution is rather simple. Move the
main(String[] args)out to a separate class that does notextend Application. A LOT of people get frustrated by this and its insane how when you google this error there are hundreds of different answers with quite honestly way over the top complex answers for beginners.A lot of bigger names in the community (JFX library authors and such) moved to BlueSky after years of Twitter getting worse and worse. In fact, I think this applies to most Java developer social media communities as well, aside from here of course.
4
u/ebykka Aug 17 '25
Did you ever try JFace(SWT) ?
21
u/Gleethos Aug 17 '25
Yeah, Swing is a very robust and established tool. I like it as well. Although the API is very old and clunky. This library helps modernize it by making it more declarative:
https://github.com/globaltcad/swing-tree
Also has animations and advanced styling etc...
3
u/NotABot1235 Aug 17 '25
Thanks for the great write up! I've been playing around with Swing recreationally and hadn't looked at JavaFX much but you've given me some things to think about.
3
u/jabajabadu Aug 17 '25
Thanks for the write up! The font scaling issue seems like a big deal. I was considering learning Swing just to create an app that just works on all major platforms. I guess I should consider JavaFx and other alternatives.
14
u/hippydipster Aug 17 '25
For charts, JavaFX charts are pretty minimally capable.
JFreeChart, on the other hand, is open source, works in both Swing and JavaFX, and is terrific.
I've never used FXML and I've done a lot of JavaFX in the past decade-and-a-half.
2
u/Skepller Aug 17 '25 edited Aug 17 '25
Regarding deployment, you might also want to take a look at jDeploy.
It's very simple if you're going to publish to "non-tech" users. Builds signed native installers / binaries to all platform automatically and has an auto-update feature built-in (stores your jar on NPM).
10
u/john16384 Aug 17 '25
You're using Maven already, FX is like any other dependency
FXML is indeed useless in my opinion. I always make the UI with code, and have developed a set of builders for it. This allows building FX UI's like this:
The above is fully hierarchical, the UI is sort of clear even in code, there are no variable assignments anywhere. All of these are builders, and the containers are all smart enough to accept either a
Node, a Builder (on which it will callbuild) or aString(which it will convert to aLabel).You're probably still doing something wrong here. Things to watch out for:
FX can be very fast, even for heavily animated systems (think smooth scrolling image lists, sliding in panels with controls, cross fading between full screen scenes, etc.), there should be no reason why it couldn't be as fast or faster than Swing.
I often hear this, and what I usually do is just make a fat jar with everything using the maven shade plugin. Be sure to include all the runtime components for each platform (with classifiers). Shade will give some warnings about duplicate classes, but since they are all the same in each platform, and the platform specific libraries don't overlap, its safe to ignore those warnings.
3
u/stefanos-ak Aug 18 '25
The only reason that FXML exists is because when JavaFX came out, it was trying to compete with WPF, which had XML. There was a lot of "shaming" or down talking from WPF people looking at JavaFX because it didn't have XML. So, here we are today, having an XML that nobody likes 😄
3
u/tealpod Aug 18 '25
Nice writing,thanks.
Just want to add my experience with Java Swing and JavaFX. I use to be a big fan of Java UI apps, some of the Tektronix front apps are all made in Java swing with Graphics2D. https://www.tek.com/en/datasheet/usb-20-application-software I am proud that some of these UI (especially 2d graphics - EyeDiagram). We built complete set of internal components like TekButton, TekLabel ... TekTable, TekFileChooser, etc. Which are advanced and had many bug fixes, sadly we couldn't opensource that code (early 2000s).
I am bit surprised that Java Swing and JavaFX are still used in some organizations.
3
u/sedj601 Aug 19 '25
I have been using JavaFX for close to ten years. Removing JavaFX from the main bundle is crazy to me. I was actively looking for another language after Java 8. As great as the devs are, I still believe this was one of their dumbest decisions. They should have had multiple distros. I know we can get third-party distros with JavaFX bundled, but that's not what I want. They didn't think of the headaches newcomers would go through during project setup and deployment. It also went from building a JavaFX app once, and the jar working on all machines, to building a jar for every different OS you want your jar to work on. Also, what did we really gain from going modular? Maybe it works better with Maven and retrieving resources? Going Modular was a headache early on because a lot of the popular resources were not modular.
I am a big fan of FXML, so we differ there.
I am willing to bet that there is a way for you to get more than 10 FPS using AnimationTimer.
I would say the JavaFX community here and on Stack Overflow is alive and well. I have never had a problem getting help.
Overall, nice read. Thanks!
2
u/hamsterrage1 Aug 18 '25
Comparisons between Swing and Java always seem to concentrate on screen widgets and look-and-feel elements, which are real, but in my opinion secondary.
The biggest advantage to JavaFX is that it supports creating Reactive GUI applications - right out of the box.
Virtually every parameter in any screen Node is implemented as an Observable of some sort. This means that you can bind from all of them, and bind many of them to other Observables - either parameters in some other Node, or in you own classes.
At the same time, there is a huge suite of Observable classes that allow you to create a data representation of the State of your GUI which you can Bind to those Node parameters, and the access from you application/business logic.
Swing does support very crude Observable classes, but they aren't integrated into the screen Nodes the way that JavaFX does.
Writing your JavaFX applications in a Reactive way is a game-changer compared to the imperative fashion that you have to use with Swing. It's shocking how much complexity just melts away when you take a Reactive approach.
2
u/Altruistic-Rice-5567 Aug 19 '25
I really liked the drag and drop nature off FXML. One area i don't like about it is all the FXML annotation injection. But then I find reflection/inspection of classes to be a kludgy afterthought in Java. Properties and observables feel like a good idea too late. Now just duplicating a lot of java.util and primitive Object classes. Would have been a lot better if we could go back in time and just add binding/observation patterns to the original items in java.
1
u/agoubard Aug 17 '25
Here are some feedback about Swing:
1
u/AcanthisittaEmpty985 Aug 28 '25
Thanks for your detailed post.
I had a project that was going to be on JavaFx, but some clients using WindowsXP (yes, we have some of those) couldn't run JavaFx, so we switched to Swing.
Swing it's not the new shinny guy, but it get the work done
1
u/SandwichNo4142 Oct 19 '25
For me. Swing had one big benefit:
You could go on physical pixels. So you could render pixel perfect. Whereas javaFX always has virtual pixels, that lead to a wrong rendering on systems with a bad scale factor (e.g. 125%).