
Topics in Part II
- Creating a model class
- Using the model class in an ObservableList
- Show data in the TableView using Controllers
Other Tutorial Parts
- Part I - Scene Builder
- Part II - Model and TableView
- Part III - Interacting with the User
- Part IV - CSS Styling
- Part V - Storing Data as XML
- Part VI - Statistics Chart
- Part VII - Deployment with e(fx)clipse
Create the Model Class
We need a model class to hold information about the people in our address book. Add a new class to the model package (ch.makery.address.model) called Person. This makes sense, since we want to manage people and their addresses. The Person class will have a few instance variables for the name, address and birthday. Add the following code to the class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
A List of Persons
The main Data that our application manages is a bunch of persons. Let’s create a list for Person objects inside the MainApp class. All other controller classes will later get access to the list inside the MainApp.
ObservableList
We are working with JavaFX view classes that always need to be informed about any changes made to the list of persons. This is important, since otherwise the view would not be in sync with the data. For this purpose, JavaFX introduces some new Collection classes.
From those collections, we need the ObservableList. To create a new ObservableList, add the following code at the beginning of the MainApp class. We’ll also add a constructor that creates some sample data and a public getter method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
The PersonOverviewController
Now let’s finally view some data in our table.
- Create a normal class inside the controller package called
PersonOverviewController.java. - We’ll add some instance variables that give us access to the table and the labels inside the view. The fields and some methods have a special
@FXMLannotation. This is necessary for the fxml file to have access to those variables. After we have everything set up in the fxml file, the application will automatically fill the variables when the fxml file is loaded. So let’s add the following code:
Note: Remember to always use the javafx imports (not awt or swing)!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
Now this code will need some explanation:
- All fields and methods where the fxml file needs access must be annotated with
@FXML. Actually, only if they are private, but it’s better to have them private and mark them with the annotation!. - The
initialize()method is automatically called after the fxml file has been loaded. At this time, all the FXML fields should have been initialized already. - The
PropertyValueFactorythat we set on the table colums are used to determine which field inside thePersonobjects should be used for the particular column. - The
setMainApp(...)method must be called by theMainAppclass. This gives us a way to access theMainAppobject and get the list of data and other things. In fact, let’s do that call right now. Replace theshowPersonOverview()method with the following. It contains two additional lines:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
Hook the view to the Controller
We’re almost there! But one little thing is missing: We haven’t told our PersonOverview.fxml file yet, which controller to use and which element should match to which field inside the controller.
- Open
PersonOverview.fxmlwith the Scene Builder. - Select the topmost AnchorPane in the Hierarchy.
Open Code on the right side and select the
PersonOverviewControlleras controller class.

Select the TableView and choose under Properties the
personTablefield as fx:id.

Do the same for the columns and select
firstNameColumnandlastNameColumnrespectively.- For each label in the second column, choose the corresponding fx:id.
- Important: Go back to Eclipse and refresh the entire AddressApp project (F5). This is necessary because Eclipse sometimes doesn’t know about changes that were made inside the Scene Builder.
Start the Application
When you start your application now, you should see something like the screenshot at the beginning of this blog post.
Congratulations!
What’s Next?
In Tutorial Part III we will add more functionality like adding, deleting and editing Persons.
Download
Source of Tutorial Part II as Eclipse Project