In this activity you will learn about the concept of data binding in JavaFX. Data binding allows you to set up a relationship between two variables so that if one of the variables changes, the other one is updated automatically. It is a somewhat advanced concept, and it's unlikely that you've seen it in any other programming languages that you may know. The examples in this activity will show you how to use the data binding feature, but it will be up to you to think about when it might be useful.
One example of where you might use data binding is to keep the state of your application in sync with its user interface. Let's consider something like a music player. The player may display a list of songs, and when the user clicks on a song, the song title is highlighted and it begins to play. The code could be pretty straightforward; when the code receives the "click" action, it highlights the line, and the finds the corresponding song file and starts playing it.
So, what happens when the song ends? One thing that could happen is that the song player goes to the next song in the list, and starts playing it. But since there is no user clicking the song title, the code mentioned earlier that does the highlighting when it receives the click action will not be called. This might be a good time to use binding: whenever a song starts to play, the code could set a variable that stores that song's name as the "active song". And then, we could create a binding between the active song variable, and the user interface object that is displaying the song list. Every time the active song changes, the user interface object could automatically highlight the title of that song. So now, it doesn't matter whether the active song is set in response to a user click, or it gets set as a result of a song ending and the player automatically starting the next song in the list.
This is just one example of a use of data binding. One thing to keep in mind, there isn't anything in JavaFX that REQUIRES data binding; data binding is just a convenience that allows you to set up relationships between variables and let JavaFX take care of responding to changes in one of the variables. It's a nice convenience for the programmer, but if it's not obvious to you when and how to use it, don't worry.
In this activity, you will:
- work with a small example program that demonstrates data binding
Skills you will learn include:
- understand the data binding syntax in JavaFX
- understanding when and why you might use the data binding feature
- Open NetBeans, and create a new project. Under the File menu, select New Project, and go through the new project wizard, creating a JavaFX project. Remember, if you need help, use the forum! Once you have created the project, you can make it the Main Project to make it easier to perform your edit/build/run cycle.
- Now go to Lesson 5: Applying Data Binding to UI Objects in the JavaFX GUI Applcation tutorial. Go through the tasks of this lesson.
- Submit your project that you created to the activity. You will submit a jar file (a java archive file) that contains your runnable program. To find the jar file, open your project in NetBeans, and note that in the project pane, there are 3 tabs: Projects, Files, Services. Select the Files tab. Expand your project folder by clicking on the '+' sign. You should see a folder called 'dist'. Expand the dist folder, you will probably see a few files and maybe some folders. One of the filenames should end in .jar. This is the file that you will submit. Open the context menu on that file (on Windows, right-click on the file; on Mac, Ctrl-click on it), and select 'properties' from the menu. One of the properties will be the full path to where the jar file lives on your system. Use your file browser to go get the file, and submit it.