In this tutorial we will look at how to create custom controls in JavaFX and how to import them into the Gluon Scenebuilder for use.
Before we get started, lets look at what custom controls are, and when you might need them.
JavaFX, like most GUI design frameworks comes with built in, reusable controls such as buttons, text boxes, checkboxes, etc.
However, sometimes we would like to be able to make a component specific to our own needs that we can then re-use in an easy way.
One classic example of this might be a TextField that has built in user input validation specific to your dataset.
There are several different ways to do this, but the one we are discussing in this tutorial is by making a custom control.
According to Jonathan Giles (One of the JavaFX Architects) the recommended way to create a custom control is by extending the layout container. Specifically, inherit from “Pane” or something that is a “Pane” (AnchorPane, GridPane, HBox, VBox, etc).
The process looks something like this.
1) Determine the desired functionality
2) Determine the layout container to use
3) Create new class that extends the layout container
4) Add controls and functionality to the layout container
5) Reuse “custom control” as desired
Now, most examples you will see of custom controls will show a Pane where the control layout as well as the specific desired behavior is defined in the .java class.
This can be fine if all you want is to extend something simple (like a text field with input validation).
However, if you have a custom control that where you are trying to piece together multiple controls, doing the layout in a .java class by hand is not my favorite way to spend an afternoon.
So, what are our options? Well. We could do our layout in the .FXML file as usual, however things get a little tricky here with having a controller and a custom control.
What does work is if you create your fxml file and controller like normal, and then ADD them to the custom control. You may need to expose widgets from the controller to the custom control so that they can show up and be manipulated at a higher level (Like Scenebuilder).
This tutorial will explore creating a JavaFX custom control from scratch and then using it in your application.
Check out the full blog (and source code) here!
https://noblecodemonkeys.com/?p=5895