I can't seem to find any material on the subject. To give a more concrete example, let's say I want to create a simple component that combines a checkbox and a label. Then, populate a ListView with instances of this custom component.
UPDATE: see my answer for complete code
UPDATE 2: For an up-to-date tutorial, please, consult the official documentation. There was a lot of new stuff that was added in 2.2. Finally, the Introduction to FXML covers pretty much everything you need to know about FXML.
UPDATE 3: Hendrik Ebbers made an extremely helpful blog post about custom UI controls.
Quick answer is <fx:include> tag, however, you would need to set the ChoiceModel in the Controller class.
Update: For an up-to-date tutorial, please, consult the official documentation. There was a lot of new stuff that was added in 2.2. Also, the Introduction to FXML covers pretty much everything you need to know about FXML. Finally, Hendrik Ebbers made an extremely helpful blog post about custom UI controls.
After a few days of looking around the API and reading through some docs (Intro to FXML, Getting started with FXML Property binding, Future of FXML) I've come up with a fairly sensible solution. The least straight-forward piece of information I learned from this little experiment was that the instance of a controller (declared with fx:controller in FXML) is held by the FXMLLoader that loaded the FXML file... Worst of all, this important fact is only mentioned in one place in all the docs I saw:
So, remember, in order to programmatically (from Java code) obtain a reference to the instance of a controller that was declared in FXML with
fx:controller
use FXMLLoader.getController() (refer to the implementation of the ChoiceCell class below for a complete example).Another thing to note is that Property.bindBiderctional() will set the value of the calling property to the value of the property passed in as the argument. Given two boolean properties
target
(originally set tofalse
) andsource
(initially set totrue
) callingtarget.bindBidirectional(source)
will set the value oftarget
totrue
. Obviously, any subsequent changes to either property will change the other property's value (target.set(false)
will cause the value ofsource
to be set tofalse
):Anyway, here is the complete code that demonstrates how FXML and Java can work together (as well as a few other useful things)
Package structure:
FxmlMvcPatternDemo.java
MainView.fxml
MainView.properties
MainController.java
ChoiceView.fxml
ChoiceController.java
ChoiceModel.java
ChoiceCell.java
For JavaFx 2.1, You can create a custom FXML control component by this way:
Component code:
MyComponent.java
MyComponentController.java
myComponent.fxml
This code needs to check if there is no memory leak.