corner imagecorner image
IDEPlatformPluginsDocs & SupportCommunityPartners

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

Content on this page applies to NetBeans IDE 6.9

To follow this tutorial, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE Java download bundle
Java Development Kit (JDK) JDK 6 Update 7 or later.
JavaFXComposer plugin  
JavaFX SDK  

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:

  1. Choose File > New Project. The New Project wizard opens. Under Categories, select JavaFX. Under Projects, select JavaFX Desktop Business Application and click Next.
  2. The Name and Location page opens. Name the project BoundList. Select a location for the project. Accept the default options and click Finish.
    Name and Location page for a new JavaFX desktop business application, showing name and default values
  3. 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.
  4. 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.
  5. Use the mouse to drag the components on the screen until they are aligned above each other. The final result should look like this:
    Aligned UI components
  6. Select the top label. In the Properties window, change the Text from "Label" to "Enter comma-separated items:".
    Properties of label showing new display text
  7. Select the lower label. In the Properties window, change the Text from "Label" to "Parsed items:".
  8. Save your changes. The final design should look something like this:
    Final design appearance

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:

  1. 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.
    Properties window for List View with Items 'open details' button highlighted
  2. The Details panel opens for the list view's items. Click the Bind button in the upper right corner.
    Details for the list view's items, with the Bind button highlighted
  3. 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:

    Bind property panel with the component, component property, and converter chosen

    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.

  4. Click Close. Your application is now complete.

Running the Project

Run the BoundList project. (F6 or Run Main Project icon 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.

Running project showing items

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: