My JavaFX tutorials have been so popular that I’ve decided to spin up a JavaFX specific site. There really isn’t enough information out there on how to use JavaFX, so I’ve introduced the new domain, https://whatisjavafx.com to answer the burning question, “What is JavaFX?”
JavaFX is a great technology, but it isn’t as easily accessible as Swing and AWT were. One of the challenges with JavaFX is that lots of set up goes into even the most simple “Hello World” style JavaFX application. Despite wonderful tools like Scene Builder and API’s, it’s tough for coders new to JavaFX to get a foothold on the ins and outs of it.
I will mirror most of the JavaFX information on this site over to https://whatisjavafx.com, except I’ll make the categories more granular. I’m planning currently to break the categories down into FXML, JavaFX 3D, JavaFX Applications, JavaFX Dialog, JavaFX Pane, JavaFX Scene, JavaFX Tutorial, and not-to-be-forgotten Scene Builder. (I absolutely LOVE Scene Builder!)
Learning JavaFX 8 on Eclipse is simple, once you get past the first Hello World style example. This JavaFX tutorial introduces SceneBuilder, FXML, and JavaFX 8 for Eclipse with an event (an ActionListener) and a controller class.
Believe it or not, I have a JavaFX 8 book that never once explains how to hook up events between a POJO and FXML generated from SceneBuilder—hundreds of pages of how-to material on JavaFX, and not one word on events. Pretty sad.
No such letdown here.
This tutorial demonstrates and explains creating a window that displays a button and a textfield in JavaFX 8, and then hooking them up in Eclipse, using SceneBuilder, so that clicking the button results in the words “Hello World” displaying in the textfield.
JavaFX allows primary stages to swap out scenes. However, JavaFX stages don’t handle MenuBars directly. So what if you want to swap JavaFX scenes, but need to share one MenuBar between them? The trick is using a BorderPane as a root pane of your scene to hold a top JavaFx MenuBar and a central pane that gets swapped, instead of swapping the primary stage’s scene.
This example creates a Main class, a menu controller class, and three FXML files. The FXML files are a MenuBar, and two AnchorPanes that we swap between. All the custom code is included below for quick reference.