Data Binding With JavaFX Composer
Written by David Kaspar, adapted and maintained by Jeffrey Rubinoff
JavaFX lets you bind, or link, attributes so that when one attribute changes, all attributes bound to it also change. This tutorial shows how to define JavaFX binding when using the JavaFX Composer.
During this tutorial you will create an application that allows users to enter a comma-separated list of strings. The user's input is automatically parsed and loaded to a list view using JavaFX binding.
Contents
To follow this tutorial, you need the following software and resources.
Designing the UI
To design the UI, first you create a new JavaFX project using the "JavaFX | JavaFX Desktop Business Application" project template. Then you add UI components and edit them to display what you want.
To design the UI:
- 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 BoundList. 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. Look for a Palette window and a Properties window on the right. If either window is missing, open it by going to Window > Palette (Ctrl+Shift+8) or Window > JavaFX Properties, respectively.
- In the Palette window, expand the JavaFX Controls section and drag the following components into the Design:
- Label (label) - The label for the input text box.
- Text Box (textBox) - Will be used for user's input.
- Label (label2) - The label for the list view that shows the results.
- List View (listView) - Displays the parsed results.
- Use the mouse to drag the components on the screen until they are aligned above each other. The final result should look like this:

- Select the top label. In the Properties window, change the Text from "Label" to "Enter comma-separated items:".

- Select the lower label. In the Properties window, change the Text from "Label" to
"Parsed items:".
- Save your changes. The final design should look something like this:
Binding List Items To User Input
In this section, you bind the list items in the List View to the text that the user enters in the Text Box.
To bind the data:
- Select the List View component in your Design area. Its properties open in the Properties window. Click on the button on the right side of its Items property.

- The Details panel opens for the list view's items. Click the Bind button in the upper right corner.

-
The Bind panel opens. For now ignore the bind field and the buttons. Note the three lists:
- Components
- Properties and expressions of selected component
- Converters
You need to bind the list items to the text property of the textBox component, and convert the comma-separated list of String values to a String[] array. First, select the textBox component in the Components list. This displays the list of that component's properties. Select the text property. Lastly, select the String,... -> String[] converter. The Bind panel now looks like this:
If you want to bind a property to a component that does not appear in the list, write the bind expression manually in the bind field and click the Generate button. This button creates a new bound function in the source code to which the property is bound. You may find it useful to write complex binding expressions manually and edit them in the source editor.
- Click Close. Your application is now complete.
Running the Project
Run the BoundList 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 final application should look like the picture below. If you type a comma-separated list of items into the text box and press Enter, the list view refreshs to display the items in the text box.
More Exercises
See Tutorials and Training at the JavaFX site and the Introduction to Binding in JavaFX tutorial at Planet JFX.
See Also
For more information about using NetBeans IDE to develop JavaFX applications, see the following resources: