Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX 2.2 control from scratch

Does anyone know why there are so much examples in Internet about creating your own custom control in JavaFX using SkinBase and BehaviorBase classes from com.sun? Why do they used? Why in FXEnsemble there is no such implementation? Are there any Best Practices for this? Can you give, please, some explanations why I should use BehaviorBase and SkinBase for custom control? Thanks in advance.

UPDATE:

I came across this video - http://www.parleys.com/#st=5&id=2789&sl=1 , which refers to the creation of custom controls. Author says about Public(Control & Skin) and Private API, and in fact Behavior is a Private API. Is it appropriate to use BehaviorBase and SkinBase from com.sun, which are not described in the official documentation?

UPDATE 2:

SkinBehaviour and JavaFX 8: http://mail.openjdk.java.net/pipermail/openjfx-dev/2013-February/005874.html

like image 976
Dmitry Avatar asked Aug 18 '12 11:08

Dmitry


2 Answers

Original Answer

There is a critical feature request to improve support for building custom controls targeted for delivery for the next major JavaFX release (JavaFX 8.0/JDK 8.0).

Until the BehaviourBase and SkinBase APIs are made public and other work required to build your own custom controls (such as definition of custom css attributes and pseudo classes) is exposed as public API, the best advice regarding use of these classes is by Richard Bair, the JavaFX platform lead:

If your intent is to write a new control for submission to openjfx . . . then definitely use these classes. If you are writing an application that is meant to work with future versions of the platform and you don't want to break, then don't use these classes.

In any event, use of the BehaviourBase and SkinBase APIs is probably best targeted for controls being built by controls library creators (such as those building for the open-jfx project for inclusion into the core JavaFX api, or the jfxtras project linked in a previous answer). Many re-usable custom application components don't require the complexity introduced by using these classes.

Unless you are creating a generic platform control for a library, you don't need to subclass Control are likely better off taking one of the following routes:

  1. Use an existing control and customize it via CSS events and it's public API.
  2. Subclass an existing control to perform more radical changes.
  3. Create your own class which subclasses a container such as a Group or a layout Pane subclass. In your subclass handle the layout and encapsulate the behaviour of multiple JavaFX nodes and controls.

For examples of creating a custom, reusable component from a Container, see the custom component section of the Introduction to FXML document. For an example of subclassing an existing control see the DigitalClock class in this example of a JavaFX clock.

Update

After I wrote this answer, I reviewed the control creation presentation linked from the updated answer and it makes the same recommendation as this answer. I'd recommend the presentation to anybody interested in the topic of JavaFX control creation.

Update Dec 2014

Much of the work has been done in Java 8 to create public APIs for creating controls using JavaFX framework classes. So it possible to build your own custom controls which extend Control or a Control subclass and make use of SkinBase. Such controls are then similar in structure and API to the built-in JavaFX controls such as Label, Button, etc. Some of information on how to do this is documented in an informal way on the open-jfx wiki, though Oracle has not yet provided complete official documentation on creating such controls on the main JavaFX documentation page.

One part of the API which was not finalized as a public API is the BehaviourBase class generally used for handling some cross platform specific implementations for tasks such as accelerator key commands for controls, however you can still use the base JavaFX key handling events or third party libraries such as ControlsFX Action to implement such behavior so that your code does not need to use private API which might change in a future Java release. I believe the BehaviourBase functionality is currently scheduled to be moved to public API classes in the Java 9 timeframe.

As stated in the original answer, unless you are creating a generic platform control for a library, you don't need to subclass Control (this is true regardless of the new control creation facilities in JavaFX 8 or 9).

Update Mar 2016

For Java 9, a Java Enhancement Proposal (JEP) and related Java bug tracker issue has been created:

  • JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization
  • JDK-8076423

The purpose of this effort is to:

Define public APIs for the JavaFX UI controls and CSS functionality that is presently only available via internal APIs and will hence become inaccessible due to modularization.

Further information on the API changes for custom control support is provided in the linked JEP and bug tracker documentation.

An early access release for Java 9 which includes the new API changes is available for download and testing.

Note that a public Behavior API is not part of Java 9 and is currently scheduled for implementation in Java 10:

  • JDK-8091189 Move BehaviorBase into public API

Other resources

If you would like a deep resource on custom JavaFX controls, you might wish to review Hendrik Ebbers book on the subject (I have not read this book):

  • Mastering JavaFX 8 Controls (Oracle Press)
like image 146
jewelsea Avatar answered Nov 14 '22 07:11

jewelsea


You might want to take a look at the JFXtras project (http://jfxtras.org). There you will find a lot of custom JavaFX controls that will use SkinBase and BehaviorBase. The source code of the controls could be found on github at https://github.com/JFXtras

like image 28
hansolo_ Avatar answered Nov 14 '22 08:11

hansolo_