Custom controls
In this section, we will look at how to create your own controls and provide different skins for them.
Skins
In JavaFX, controls were created with the Model-View-Controller pattern in mind. The Control
class itself is responsible for the Model/Controller part—it holds data, state, and logic to work with those. Each control has a corresponding Skin
class which is responsible for the view—the visual representation of the control.
Note
There is also a BehaviorBase
class that provides an option to split the Model and Controller of the Control
, but unfortunately, it's an internal API. Maybe, JavaFX developers will make it public in future releases.
ClockControl demo
Let's see how skins work using our ClockControl
demo. We will create two different skins for it and will provide an option to switch between them.
Our skins will be called Hands and Text—let's start with the control that will manage them and other ClockControl
logic. Note the next points:
- As we are making a proper control...