Issue
In a javafx project I forked, the class BehaviorSkinBase
from the package com.sun.javafx.scene.control.skin
is used. This is an old project so I expected some code to be deprecated but this class doesnt appear in the deprecated list.
On this website, I found the entire documentation about the missing class, but there is no source code or javafx version.
I tried to build the project with multiple version of javafx from the official website but none of them solved the problem.
This class is used in order to define a custom slider with other javafx class.
public class CustomSliderSkin extends BehaviorSkinBase<Slider, SliderBehavior>
Is there another way of doing this sort of customization if this class is definitely no longer usable ?
Solution
Use of internal com.sun
APIs are not publicly supported in JavaFX
BehaviorSkinBase
was com.sun
api.
It was never supported as public API, so it could change without warning or deprecation (which it did).
It is best for any user application code to not rely on BehaviorSkinBase
, or any other com.sun
API.
The above information is the actual answer to this question.
The information below is informational on JavaFX custom controls and implementation options, so can be ignored if not relevant to you at this time.
Implementation Options
Is there another way of doing this sort of customization if this class is definitely no longer usable?
Yes, there are multiple options, see the steps below.
The decision of which approach is best for your application will depend on the amount of customization you wish to do, as well as your skill level and the amount of time you wish to expend on the effort.
How to re-write your custom slider
Compare the custom slider to the standard JavaFX slider, if you can just use a standard slider instead of a custom one, then do so.
If the only changes in your custom slider are visual styling, perhaps providing some custom CSS to style the standard slider will suffice.
If the custom slider has a different node hierarchy from the standard slider, then likely just styling the standard slider won't cut it, you will need to create a custom skin.
If you need to create a custom skin, then see if you can subclass SliderSkin, this will allow you to provide a different node hierarchy and completely different visuals for the Slider, while still retaining the public API for the Slider class.
If your custom slider requires a different public API from the slider class, then create a new Control subclass and a new Skin subclass (you are now creating a completely custom control from scratch).
Note that custom controls are often good for a general-purpose control library, but, often, for a typical application are not needed because the application doesn't require the formality of the control interface and can instead define its UI using layout panes and collections of in-built controls integrated together using a controller and perhaps defined using FXML. So most application code gets by with just step 1 or 2 of the above list (e.g. using a standard slider perhaps customized in look via CSS).
Handling behaviors in your customized control
If you used the existing slider skin it comes with its in-built behavior implementation which is internal and not public API but is likely sufficient for you and you are unlikely to need to change it.
If you had to create your own completely custom control, then it will not have a behavior implementation, so you will need to add your own from scratch. The behavior implementation will be doing the keyboard and input handling for the control and maybe some other work.
If it is a simple control with simple behavior, this will be as simple as attaching a few event handlers to the nodes in the control.
If it is a complex control that has different key mappings for different platforms, then perhaps take a look at the internal behavior implementation in JavaFX to see how it implements behavior modeling and get some ideas from that (or copy-and-paste relevant stuff to your own app codebase), to help with implementing the behavior for your control.
Historical internal implementation link Behavior and Skin
See custom controls wiki change 4.
Change #4: SkinBase no longer has any public reference to Behavior API.
That was a change from JavaFX 2 to 8. Likely the code you are working with is based on a JavaFX version that is long obsolete. Read also about the skin at the same link, it might help you.
Background Info on Behaviors
If you are curious, the current API behavior code is at:
It doesn't include BehaviorSkinBase as, long ago, the concepts of behaviors and skins were separated in JavaFX. The behavior (as far as I understand it) is mainly for handling keyboard input mapping (though it likely can do other things too). A lot of those input mappings are target platform-specific, so a different mapping might be used for a Mac user versus a PC user, which allows the keyboard input to conform to the standard keyboard usage for the target platform. The behaviors (I believe) help handle those kinds of idiosyncrasies.
Background Info on Skins
The skins represent the visual layout of a control. The Control subclass defines the public API, and is often abstracted from the internal implementation of the Skin. This allows you to define a control like a Spinner which might feature up and down arrows to change an integer value within a range on a PC, but on a touch device like an iOS, it might be implemented as a scrollable rolodex style. The public control interface doesn't change, but different skin implementations are attached to the control for different platforms to allow a different look and feel depending on the target device.
Custom controls that you create from scratch can extend the Skin interface to allow them to be skinnable as described above.
Also, the Skins of all the in-built controls in the JavaFX platform are available as public API in the javafx.scene.control.skin package. So if you want to provide a new skin for an existing control, you can extend and override an existing skin implementation (which will be a lot easier than creating a custom control from scratch as long as the control you are customizing has functionality close to your desired control).
Background on Behavior/Skin Integration
Internally, the skins can reference behaviors for additional customization, e.g. different key accelerator input maps for different platforms as previously discussed.
The JavaFX platform implements an internal behavior framework and system to support the public API controls in the core platform. That behavior framework is completely private API though (in com.sun
) classes, and so is subject to change without notice.
You should never write code that depends on com.sun
classes from the JavaFX framework, as the next time the framework is released for a new feature or bug fix, it could break your code, unlike the public API which is stable and provides advance notice of any deprecated features.
So not relying on the internal behavior framework in JavaFX is probably a good thing. You can, however, implement your own behaviors for your custom controls if you need them. To do so, you could look at the implementation in the openjfx source for the in-built controls and emulate the pieces you wish (either through copy-and-paste of private API to new classes in your project or by implementing the input behaviors for your custom controls from scratch).
Note that if your custom control is just a grouping of in-built core controls from the JavaFX platform or a subclass of one of those controls, then it will inherit the default behaviors of those controls. Often this will be sufficient and you will not need to code your own behaviors.
Answered By - jewelsea