
Taking a look at how EventHandlers, Listeners, Subscriptions and Bindings are different, and how they should be used in a JavaFX application.

JavaFX is a software platform and a graphical user interface (GUI) toolkit that allows developers to create rich and interactive applications for desktop, mobile, and embedded devices. It provides a powerful set of tools and APIs for building modern, visually appealing applications.
JavaFX was introduced as a successor to the Swing framework. It is designed to provide a more advanced and flexible way of developing user interfaces compared to Swing, with improved graphics and multimedia capabilities.
Rules
Taking a look at how EventHandlers, Listeners, Subscriptions and Bindings are different, and how they should be used in a JavaFX application.
When I started using JavaFX we were coming from having worked (for a short while) with Swing. Our approach to JavaFX was to use it just like it was Swing, but with different widgets and method names.
This meant that we wrote code that loaded data into screen nodes, and then we had to scrape it out again in the code that ran when the "Save" Button was clicked. When we had nodes that were dependent on other nodes, then we had handlers that ran when focus was gained or lost, so that we could update those other nodes. Or key listeners that would run when "Enter" was pressed.
Stuff like that.
Eventually we learned about Bindings, and we started to develop "rules" about how we would use Bindings. These were things like: When you had multiple properties in your layout that relied on some element in your Presentation Model, bind them all to that property in the Presentation Model, don't chain them off each other. For instance, if we had 3 Buttons that needed to be visible together based o
New Binding and Listener Methods in JFX 19/21
You might have missed it, but there were some significant new features added to JavaFX in the 19 and 21 releases. These all relate to Observables
, and make creating Bindings
and Listeners
easier to use.
I don't think enough noise was made about these when they came out, and I totally missed them until a little while ago. I spent some time experimenting with them, looking at the source code, and then wrote an article to explain just about everything you need to know:
https://www.pragmaticcoding.ca/javafx/subscribe_and_map
First, we now have some methods to create Subscriptions
on Observables
, and these are way easier to use than ChangeListener
and InvalidationListener
. Basically, Subscriptions
are wrappers around the Listeners
, so the same notification mechanism is used "under the hood", but they are easier to declare and manage.
Secondly, a new method has been added, ObseravbleValue.map()
. This is super cool, and allows all kinds of conversions and calculations
Article: Understanding ListView
This is the first of two articles about ListView:
https://www.pragmaticcoding.ca/javafx/elements/listview-basics
Personally, I'm a big fan of ListView, and a big fan of using it to do really cool stuff where you treat it more like a scrolling bunch of layouts. The team I worked with for years always wanted to build TableViews, so it was an on-going battle to try to get them to do more cool ListViews (that I mostly lost).
Anyways, you have to start at the start, and this article handles just the basics about ListView.
Take a look and let me know what you think.
Article 2 is just about done and covers most of the things you'll need to know to create cool layouts. Originally this was all one big article, but when I took a look at after a few days away, it was just getting to big and overwhelming.
In this article, we taka a look at how TableView handles data, the mechanism that move data in and out of TableCells, and how to work with it.
After finishing the TableView Basics article, I thought it was a good idea to keep on going with the next logical TableView topic: how to handle data coming into your TableCells.
I think it's best if you view a TableCell just the same way you would any other layout. In other words, create a static layout that behaves dynamically in response to changes in the underlying data model. This is conceptually a little bit more complicated with TableCell because that data model is constantly replaced with new versions of the data model as the TableView is populated and the users scroll through it.
Usually, you don't see this complexity because you have a data model for the TableCell that's just a single value. But if you want to have a single column column in your TableView show data from several different elements in your TableView data model, or if you want to have TableCells that display data from a more complicated TableCell data model, then you need to have a better understanding about h
Getting Started with TableView
What is a TableView and how do you use it? Hereβs enough information to get you started with TableView the right way.
I had this article sitting around for the longest time (like over a year) and just couldn't get around to finishing it up until someone asked me a question related to it. That's why it's in Java and not Kotlin.
This was (still is) intended to be the first of a series of articles on TableView and really covers just the standard stuff you can find in most online tutorials. However, I've tried to go a little bit deeper into explaining how and why stuff works than you'll find in those other tutorials. So, even though it feels to me a bit like, "the article you have to write before you can write the articles about the fun and cool stuff", I think it's going to be a better place to start if you aren't familiar with the basics about TableView.
Future articles are going to talk about creating custom columns, combining data fields into a single column, fancy row stuff and styling aspects.
Anyway, take a look
Printing from JavaFX
In today's digital world, generating and printing reports and documents are still essential tasks in various industries. JavaFX, the popular Java framework for building rich desktop applications, provides a powerful set of tools for creating, manipulating, and printing documents. In this article, we...
Over the years, I've seen a lot of people ask, "How can I print this TableView?", or some other similar question. I've always thought this was a bit silly, a TableView
is a screen thing (or a VBox
, or whatever), and printing it doesn't make much sense.
Most of the answers I've seen to these questions are along that line - printing is a data-centric thing and you should do it from the back-end as it has nothing to do with your GUI.
But it turns out that JavaFX does have classes designed to enable printing of screen layouts! Go figure.
Edward Stephen Jr. has an new article out introducing the concept. I think it's worth a read.
I still don't know what happens if you print a TableView
, but at least there is a way to find out.
Creating a Custom Skin
An introduction to JavaFX Skins and Skinnable and how to create your own skin.
I have a blog where I talk about programming stuff - mostly JavaFX and Kotlin. I've been trying to keep up a steady stream of new content, and I have lots of ideas for topics, but sometimes life gets in the way. This is the first article I've posted in a couple of months.
This latest article is the last in a set of three about creating custom controls in JavaFX. The first two were:
They're not actually required reading for this last article, but they do cover, between them, the most common use cases and approaches for creating custom controls.
But if you've ever wondered what Skin
was, and how you might alter the look and feel of one of the standar
JavaFX Resources
Which are some of the resources you use for JavaFX? Below are some of the best ones I've found:
How can I make a dead simple program in Java FX?
I'm intimidated by the UI but the allure of cross platform UI draws me in. How does one get started?