JavaFX Composer Fragments Tutorial
Written by David Kaspar, adapted and maintained by Jeffrey Rubinoff
By using JavaFX fragments, you can create parts of your GUI design in separate files. You then embed these fragments into your Main.fx master file. JavaFX Fragments simplify the creation of complicated GUI applications, because you do not have to create and configure all UI components in a single file.
JavaFX Fragments are an advanced feature. Be certain you understand templates, states, properties, and generated source code before you attempt to use Fragments. See the basic tutorials on the JavaFX wiki.
Contents
To complete this tutorial, you need the following software and resources.
Creating an Application with a Fragments File
For the purposes of this tutorial, you need a JavaFX Desktop application.
To create the application and a Fragments file:
- Choose File > New Project. The New Project wizard opens. Under Categories, select JavaFX.
Under Projects, select JavaFX Desktop Business Application and click Next.
- The Name and Location page opens. Name the project Fragments. Select a location for the project. Accept the default options and click Finish. The project is created and an empty design file opens in the Editor.
- Right-click the Fragments project node in the Projects window. Select New > JavaFX Fragments Design File. (If this option does not appear, try New > Other. The New File wizard opens. Expand the JavaFX category.)
- The New JavaFX Fragments Design File wizard opens. Name the file Fragments and place it in the existing fragments package.

- Click Finish. The new design file opens in the editor.
Design Fragments View
The Navigator window has no "Design File > scene [Scene]" node. Instead, there is a "Design File > Design Fragments" node. Select this node in the Navigator window or in the background of the Design view. The Design view displays "Design Fragments" as the edited scope. The viewport switches to the "Desktop Infinite" profile. The Properties window shows properties of the "Design Fragments" node. The properties of the fragments design file will be described later.
Source Code
Switch to the Source tab and expand the folded code. The Fragments class currently represents an empty design.

The code generated in the guarded section (on a gray background) has neither a scene variable nor a getDesignScene function. You can generate a scene variable or a getDesignScene function in the Properties window for the Design Fragments. This converts the Design Fragments file to a Main design file. See Interconverting Fragments Design Files and Main Design Files.

The run script-level function is still present. This function constructs a new Stage and a new Scene and calls the getDesignRootLevelNodes function in the Fragments class instance.
The run function allows you to quickly run and see the designed fragments at runtime. To run the file, choose Run File from the context menu of the Fragments file node in the Projects window or press Shift-F6 while you are in the editor.
You can delete the run function at any time. It is not necessary for your application, because the application uses the run function in the Main design file. For the purposes of this tutorial, do NOT delete the "run" function now.
Designing the Fragments
Switch back to the Design view. In this section, you add and configure UI components to the Fragments file.
To design the fragments:
- Select the Fragments.fx file in the Projects window. Select the Design Fragments node in the Navigator window.
- Drag an Index Buttons template from the Palette and drop it into the Design view. In the Customizer window, select "currentState" as the indexed component.
- Drag a Label control from the Palette and drop it in the Design view, near the index buttons.
- Open the Details window of the text property of the label.
- Press the Bind button and select "currentState" and "Actual State Name" in the lists. Close the window.

- Go to the States view. Add new states named "Init", "Shown", and "Expand". (See the States tutorial.)
- Show the State Variables editor and edit the "currentState" to use 1000ms default duration and LINEAR default interpolator. (See the Parallel States tutorial.)
- Go to the States view. Select the "Shown" state.
- Select the label in the Navigator window or the Design view. In the Properties window, add a color to the label's Text Fill property.
- Go to the States view. Select the "Expand" state.
- Select the label in the Navigator window or the Design view. In the Properties window, click the icon
to open the Transform properties.
- Locate the Scale X and Scale Y properties. Change them both to 2.0.
- Locate the Translate X property. Change it to 15. This moves the label to the right, so it does not go off the left side of the view when it expands. Translate properties are useful with fixed layout containers such as VBoxes.

The Design Fragments file is now complete. The file has 3 states with an animation for transforming between states. The file also has 2 design fragments, a tile fragment with 2 buttons and a label fragment.

Running the Fragments File
Click the "Run File" menu action or press Shift+F6 on your keyboard to run the fragments design file. When the application starts, you see that the "Index Buttons" and Labels fragments overlap with each other, but when you were editing them in the Design view, they were positioned properly.
Note: If you see only an empty design, you probably used the "Run Project" action or pressed F6 instead of running only the Fragments file.

This is one of the main differences between fragments design files and regular design files. Fragments are meant to be used as they are, without additional shifting or adjusting, in different parts of your UI, Scene, design, and custom code. Therefore, the tool ignores the layoutX and layoutY properties of the root-level components, such as the "tile" and "label" components, and does not set these properties in the generated source code.
Although the following procedure is not recommended, if you want to run the fragments file and see the layout you designed, do the following:
- Select the Design File > Design Fragments node in the Navigator window.
- In the Properties window, set the "Generate LayoutXY for Root-level Components" property to "true".
- If you run the file again, you see the Scene exactly as it is laid out in the design view.
For the purposes of this tutorial, do NOT set the "Generate LayoutXY ..." property. Leave it set to "false".
In both cases, when you run the Fragments file, it is fully functional. You can press the Previous and Next buttons to control the Label's text.
Designing the Main File
In this section, you add UI components to the Main design file. Later, you embed the fragments into this file.
To design the Main file:
-
In the Projects window, open the "Main" file. Switch to the Design view.
- Add a VBox container to the Design.
- Add a Label to the VBox and change its Text property to "The Index Buttons Fragment:".
- Add a Panel to the VBox and change its Instance Name to "indexButtonsPlaceHolder".
- Add a second Label to the VBox and change its Text property to "The Label Fragment:" string.
- Add a second Panel to the VBox and change its Instance Name to "labelPlaceHolder".
Embedding Fragments in the Main Design File
So far the Main and Fragments designs are still disconnected. JavaFX Composer does not yet provide support for embedding design fragments in the visual editor. Therefore, you have to embed the fragments by editing the source code.
To embed fragments in the Main design file:
- Switch to the Source view of the Main design file.
-
Insert the following code into the Main class, after the generated code block:
//embed the Fragments design into the Main design
def fragments = Fragments {}
init {
// tile fragment into indexButtonsPlaceholder
insert fragments.tile into indexButtonsPlaceholder.content;
// label fragment into labelPlaceholder
insert fragments.label into labelPlaceholder.content;
}
Now you can run the entire application.
Running the Project
Run the Fragments project. (F6 or Run Main Project icon
, if it is the main project.) The design file is saved and the project is compiled and started. The buttons switch the application between states.

The "Index Buttons" and "Label" components are synchronized and the animations run as you specified in the Fragments design file. This is because the same instance of the Fragments class holds the instances of the index buttons and the label. The application would work this way even if you had used each fragment in a different Scene.
In the same Main design, you can embed multiple instances of one Fragments design or instances of different Fragments designs.
Note that the Main design can control the instances/content/state of the Fragments design. For example, you can add a new button into the Main design and assign an "action" function with the fragments.currentState.next(); code.
To clean up an application, you can delete the run function from the Fragments design file because it is not needed except for testing purposes.
Interconverting Fragments Design Files and Main Design Files
You can convert an existing Main design file into a Fragments file by removing its Scene. Try this on the Main file in your application.
To convert the Main file into a Fragments file:
- Open the Main design file in the Design view.
- In the Navigator window, select the "Design File | scene [Scene]" node.
- In the Properties window, press the "Code" toggle-button to show properties in the Code category.

- Set the Generate Scene property and the Generate getDesignScene Function property to "false".

In the Navigator window, the original Design File > scene [Scene] node is replaced with the Design File > Design Fragments node.
Now switch to the "Source" view and expand the generated code section. You can see that the Main class does not contain a definition of the scene instance or a getDesignScene function. You converted the Main design file to a Fragments design file.
Similarly, you can convert a Fragments design file to a Main design file by adding a Scene. To convert your Design Fragments file back to a Main design file, set the Generate Scene property and the Generate getDesignScene Function property to "true".
By switching the Generate Scene property and the Generate getDesignScene Function property between "true" and "false," you can easily convert files back and forth from being Design Fragments files to being Main design files.
More Exercises
You can create a new project from the Media View sample project included in the IDE.
Open this sample from the Samples > JavaFX category. This sample demonstrates the design-fragment
concept with a single Main.fx design containing the Scene and Stage and multiple design fragments.
The fragments are inserted into the Scene in the Main design.
See Also
For more information about using NetBeans IDE to develop JavaFX applications, see the following resources: